Decoding structs
Encoding Go data objects in JSON is a relatively easy task: well-defined data types and semantics are translated into a less expressive representation, usually resulting in some information loss. For instance, an integer variable and a float64
variable may be encoded to give identical output. Because of this, decoding JSON data is usually more difficult.
How to do it...
- Use JSON tags to map JSON keys to struct fields.
- Use the
json.Unmarshal
function to decode JSON data into Go data objects. The standard library uses the following conventions for basic types:
JSON Input |
Go type |
Result |
|
|
|
|
|
|
|