@@ -609,3 +609,147 @@ if err != nil {
609609}
610610fmt.Printf (" %d , %s \n " , rint, bs)
611611```
612+
613+ ## JSON
614+ ``` go
615+ package main
616+ import (
617+ " encoding/json"
618+ " fmt"
619+ " os"
620+ " reflect"
621+ )
622+ type Response1 struct {
623+ Page int
624+ Fruits []string
625+ }
626+ type Response2 struct {
627+ Page int ` json:"page"`
628+ Fruits []string ` json:"fruits"`
629+ }
630+
631+ func main () {
632+ // encoding basic data types to JSON strings。
633+ /* *
634+ :!go run a.go
635+ []uint8
636+ [116 114 117 101]
637+
638+ []uint8
639+ 1
640+
641+ []uint8
642+ 3.12
643+ [51 46 49 50]
644+
645+ []uint8
646+ "hello"
647+ [34 104 101 108 108 111 34]
648+ */
649+ bolB , _ := json.Marshal (true )
650+ fmt.Println (reflect.TypeOf (bolB))
651+ fmt.Println (bolB)
652+
653+ intB , _ := json.Marshal (1 )
654+ fmt.Println (reflect.TypeOf (intB))
655+ fmt.Println (string (intB))
656+
657+ fltB , _ := json.Marshal (3.12 )
658+ fmt.Println (reflect.TypeOf (fltB))
659+ fmt.Println (string (fltB))
660+ fmt.Println (fltB)
661+
662+ strB , _ := json.Marshal (" hello" )
663+ fmt.Println (reflect.TypeOf (strB))
664+ fmt.Println (string (strB))
665+ fmt.Println (strB)
666+
667+ // encoding slices and maps to JSON arrays and objects.
668+ /* * slice
669+ []uint8
670+ ["apple","orange"]
671+ [91 34 97 112 112 108 101 34 44 34 111 114 97 110 103 101 34 93]
672+ */
673+ slcD := []string {" apple" , " orange" }
674+ slcB , _ := json.Marshal (slcD)
675+ fmt.Println (reflect.TypeOf (slcB))
676+ fmt.Println (string (slcB))
677+ fmt.Println (slcB)
678+ /* * map
679+ []uint8
680+ {"apple":5,"orange":6}
681+ [123 34 97 112 112 108 101 34 58 53 44 34 111 114 97 110 103 101 34 58 54 125]
682+ */
683+ mapD := map [string ]int {" apple" : 5 , " orange" : 6 }
684+ mapB , _ := json.Marshal (mapD)
685+ fmt.Println (reflect.TypeOf (mapB))
686+ fmt.Println (string (mapB))
687+ fmt.Println (mapB)
688+
689+ // encoding automatically your custom data types.
690+ // {"Page":1,"Fruits":["apple","orange"]}
691+ res1D := &Response1{
692+ Page: 1 ,
693+ Fruits: []string {" apple" , " orange" },
694+ }
695+ res1B , _ := json.Marshal (res1D)
696+ fmt.Println (string (res1B))
697+ // use tags on struct field declarations to customize the encoded JSON key names.
698+ // {"page":1,"fruits":["apple","orange"]} [add tags to keys]
699+ res2D := &Response2{
700+ Page: 1 ,
701+ Fruits: []string {" apple" , " orange" },
702+ }
703+ res2B , _ := json.Marshal (res2D)
704+ fmt.Println (string (res2B))
705+
706+ fmt.Println (" DECODING JSON" )
707+ /* **************************DECODING JSON**************************/
708+ /* *
709+ 1. provide a variable where the JSON package can put the decoded data.
710+ 2. map[string]interface()/ will hold/ a map of/ strings to arbitray data types.
711+ */
712+ var dat map [string ]interface {}
713+ fmt.Println (reflect.TypeOf (dat)) // map[string]interface {}
714+
715+ byt := []byte (` {"num":6.13,"strs":["apple","orange"]}` )
716+ // check for associated errors
717+ /* *
718+ map[num:6.13 strs:[a b]]
719+ map[string]interface {}
720+
721+ */
722+ if err := json.Unmarshal (byt, &dat); err != nil {
723+ panic (err)
724+ }
725+ fmt.Println (dat) // map[num:6.13 strs:[a b]]
726+ fmt.Println (reflect.TypeOf (dat)) // map[string]interface {}
727+
728+ // situation1: none-nested. get value to get directly.
729+ num := dat[" num" ].(float64 )
730+ fmt.Println (num) // 6.13
731+ fmt.Println (reflect.TypeOf (num)) // float64
732+ // situation2: nested. requires a series of casts.
733+ strs := dat[" strs" ].([]interface {})
734+ str1 := strs[0 ].(string )
735+ fmt.Println (reflect.TypeOf (dat[" strs" ])) // []interface {}
736+ fmt.Println (reflect.TypeOf (strs)) // []interface {}
737+ fmt.Println (reflect.TypeOf (str1)) // string
738+ fmt.Println (str1)
739+
740+ // decode JSON into custom data types.
741+ str := ` {"page": 1, "fruits": ["apple", "peach"]}`
742+ res := Response2{}
743+ json.Unmarshal ([]byte (str), &res)
744+ fmt.Println (res) // {1 [apple peach]}
745+ fmt.Println (res.Page ) // 1
746+ fmt.Println (res.Fruits [0 ]) // apple
747+
748+ /* *
749+ Except used bytes and strings as imtermediates between the data and JSON represation on standard out above.
750+ We can also stream JSON encoding to os.Writer like os.Stdout or HTTP response bodies.
751+ */
752+ enc := json.NewEncoder (os.Stdout )
753+ d := map [string ]int {" apple" : 5 , " orange" : 7 }
754+ enc.Encode (d) // {"apple":5,"orange":7}
755+ ` ` `
0 commit comments