Go offer built-in support for JSON encoding and decoding, including to and from built-in and custom data types
package main import ( ) type Response1 struct { Page int Fruits []string } type Response2 struct { Page ` Fruits []` } func main() { bolB, _ := json.Marshal(true) fmt.Println(string(bolB)) intB, _ := json.Marshal(1) fmt.Println(string(intB)) fltB, _ := json.Marshal(2.34) fmt.Println(string(fltB)) strB, _ := json.Marshal() fmt.Println(string(strB)) slcD := [], , } slcB, _ := json.Marshal(slcD) fmt.Println(string(slcB)) mapD := map[: : 7} mapB, _ := json.Marshal(mapD) fmt.Println(string(mapB)) res1D := &Response1{ Page: 1, Fruits: [], , }} res1B, _ := json.Marshal(res1D) fmt.Println(string(res1B)) res2D := &Response2{ Page: 1, Fruits: [], , }} res2B, _ := json.Marshal(res2D) fmt.Println(string(res2B)) byt := []::[, ]}`) var dat map[string]interface{} if err := json.Unmarshal(byt, &dat); err != nil { panic(err) } fmt.Println(dat) num := dat[].(float64) fmt.Println(num) strs := dat[].([]interface{}) str1 := strs[0].(string) fmt.Println(str1) str := `{: : [, ]}` res := &Response2{} json.Unmarshal([]byte(str), &res) fmt.Println(res) fmt.Println(res.Fruits[0]) enc := json.NewEncoder(os.Stdout) d := map[: : 7} enc.Encode(d) }
[,,] {::7} {::[,,]} {::[,,]} map[num:6.13 strs:[a b]] 6.13 a &{1 [apple peach]} apple {::7}