This Go code demonstrates the use of the encoding/json package for encoding and decoding JSON data. Let's go through the code with inline comments and explanations:
// Importing necessary packages.
import (
"encoding/json"
"fmt"
"os"
)
// Struct definition for response1.
type response1 struct {
Page int
Fruits []string
}
// Struct definition for response2 with JSON tags.
type response2 struct {
Page int `json:"page"`
Fruits []string `json:"fruits"`
}
// The main function, where the execution of the program begins.
func main() {
// Encoding various data types to JSON.
// Encoding a boolean value.
bolB, _ := json.Marshal(true)
fmt.Println(string(bolB))
// Encoding an integer.
intB, _ := json.Marshal(1)
fmt.Println(string(intB))
// Encoding a float.
fltB, _ := json.Marshal(2.34)
fmt.Println(string(fltB))
// Encoding a string.
strB, _ := json.Marshal("gopher")
fmt.Println(string(strB))
// Encoding a slice of strings.
slcD := []string{"apple", "peach", "pear"}
slcB, _ := json.Marshal(slcD)
fmt.Println(string(slcB))
// Encoding a map.
mapD := map[string]int{"apple": 5, "lettuce": 7}
mapB, _ := json.Marshal(mapD)
fmt.Println(string(mapB))
// Encoding a struct (response1).
res1D := &response1{
Page: 1,
Fruits: []string{"apple", "peach", "pear"}}
res1B, _ := json.Marshal(res1D)
fmt.Println(string(res1B))
// Encoding a struct with JSON tags (response2).
res2D := &response2{
Page: 1,
Fruits: []string{"apple", "peach", "pear"}}
res2B, _ := json.Marshal(res2D)
fmt.Println(string(res2B))
// Decoding JSON data.
// Decoding a JSON byte slice into a map.
byt := []byte(`{"num":6.13,"strs":["a","b"]}`)
var dat map[string]interface{}
if err := json.Unmarshal(byt, &dat); err != nil {
panic(err)
}
fmt.Println(dat)
// Accessing values from the decoded map.
num := dat["num"].(float64)
fmt.Println(num)
strs := dat["strs"].([]interface{})
str1 := strs[0].(string)
fmt.Println(str1)
// Decoding a JSON string into a struct (response2).
str := `{"page": 1, "fruits": ["apple", "peach"]}`
res := response2{}
json.Unmarshal([]byte(str), &res)
fmt.Println(res)
fmt.Println(res.Fruits[0])
// Encoding a map and writing it to os.Stdout using a JSON encoder.
enc := json.NewEncoder(os.Stdout)
d := map[string]int{"apple": 5, "lettuce": 7}
enc.Encode(d)
}
Output
true
1
2.34
"gopher"
["apple","peach","pear"]
{"apple":5,"lettuce":7}
{"Page":1,"Fruits":["apple","peach","pear"]}
{"page":1,"fruits":["apple","peach","pear"]}
map[num:6.13 strs:[a b]]
6.13
a
{1 [apple peach]}
apple
{"apple":5,"lettuce":7}
Explanation:
Encoding:
Various types (boolean, integer, float, string, slice, map, and structs) are encoded to JSON using json.Marshal.
Structs and JSON Tags:
The response1 and response2 structs demonstrate how to use JSON tags for field customization during encoding.
Decoding:
JSON data is decoded into a map using json.Unmarshal.
Values are accessed from the decoded map.
Decoding into Structs:
JSON data is decoded into a struct (response2 in this case) using json.Unmarshal.
JSON Encoder:
json.NewEncoder is used to create a JSON encoder that writes to os.Stdout.
A map is encoded and written to os.Stdout using the encoder.
This code illustrates the basic usage of the encoding/json package in Go for encoding and decoding JSON data.