Json

Encoding

// Using Maps for aribitary json objects 
// or data you don't know it's tructure
mapD := map[string]int{"apple": 5, "lettuce": 7}
mapB, _ := json.Marshal(mapD)
fmt.Println(string(mapB))
----------------------------------------------

// Using Structs 
type response2 struct {
    Page   int      `json:"page"`
    Fruits []string `json:"fruits"`
}

res2D := &response2{
    Page:   1,
    Fruits: []string{"apple", "peach", "pear"}}
res2B, _ := json.Marshal(res2D)
fmt.Println(string(res2B))

--------------------------------------
// NewEncoder We can also stream JSON encodings directly to os.Writers like os.Stdout 
// or even HTTP response bodies.
d := map[string]int{"apple": 5, "lettuce": 7}
json.NewEncoder(os.Stdout).Encode(d)

Decoding

Last updated

Was this helpful?