Tutorial by Examples

json.Marshal from the package "encoding/json" encodes a value to JSON. The parameter is the value to encode. The returned values are an array of bytes representing the JSON-encoded input (on success), and an error (on failure). decodedValue := []string{"foo", "bar"} ...
json.Unmarshal from the package "encoding/json" decodes a JSON value into the value pointed by the given variable. The parameters are the value to decode in []bytes and a variable to use as a storage for the de-serialized value. The returned value is an error (on failure). encodedValue :...
JSON data can also be read from files. Let's assume we have a file called data.json with the following content: [ { "Name" : "John Doe", "Standard" : 4 }, { "Name" : "Peter Parker", "Standard" : ...
The goal with using anonymous structs is to decode only the information we care about without littering our app with types that are used only in a single function. jsonBlob := []byte(` { "_total": 1, "_links": { "self": "https://api.twitch.tv/k...
Consider the following example: type Company struct { Name string Location string } Hide/Skip Certain Fields To export Revenue and Sales, but hide them from encoding/decoding, use json:"-" or rename the variable to begin with a lowercase letter. Note that this prevents ...
As a good developer you have created following struct with both exported and unexported fields: type MyStruct struct { uuid string Name string } Example in Playground: https://play.golang.org/p/Zk94Il2ANZ Now you want to Marshal() this struct into valid JSON for storage in somet...
Let's assume we have the following struct that defines a City type: type City struct { Name string Temperature int } We can encode/decode City values using the encoding/json package. First of all, we need to use the Go metadata to tell the encoder the correspondence between the...

Page 1 of 1