|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "reflect" |
| 8 | +) |
| 9 | + |
| 10 | +type Response1 struct { |
| 11 | + Page int |
| 12 | + Fruits []string |
| 13 | +} |
| 14 | +type Response2 struct { |
| 15 | + Page int `json:"page"` |
| 16 | + Fruits []string `json:"fruits"` |
| 17 | +} |
| 18 | + |
| 19 | +func main() { |
| 20 | + //encoding basic data types to JSON strings。 |
| 21 | + /** |
| 22 | + :!go run a.go |
| 23 | + []uint8 |
| 24 | + [116 114 117 101] |
| 25 | +
|
| 26 | + []uint8 |
| 27 | + 1 |
| 28 | +
|
| 29 | + []uint8 |
| 30 | + 3.12 |
| 31 | + [51 46 49 50] |
| 32 | +
|
| 33 | + []uint8 |
| 34 | + "hello" |
| 35 | + [34 104 101 108 108 111 34] |
| 36 | + */ |
| 37 | + bolB, _ := json.Marshal(true) |
| 38 | + fmt.Println(reflect.TypeOf(bolB)) |
| 39 | + fmt.Println(bolB) |
| 40 | + |
| 41 | + intB, _ := json.Marshal(1) |
| 42 | + fmt.Println(reflect.TypeOf(intB)) |
| 43 | + fmt.Println(string(intB)) |
| 44 | + |
| 45 | + fltB, _ := json.Marshal(3.12) |
| 46 | + fmt.Println(reflect.TypeOf(fltB)) |
| 47 | + fmt.Println(string(fltB)) |
| 48 | + fmt.Println(fltB) |
| 49 | + |
| 50 | + strB, _ := json.Marshal("hello") |
| 51 | + fmt.Println(reflect.TypeOf(strB)) |
| 52 | + fmt.Println(string(strB)) |
| 53 | + fmt.Println(strB) |
| 54 | + |
| 55 | + //encoding slices and maps to JSON arrays and objects. |
| 56 | + /** slice |
| 57 | + []uint8 |
| 58 | + ["apple","orange"] |
| 59 | + [91 34 97 112 112 108 101 34 44 34 111 114 97 110 103 101 34 93] |
| 60 | + */ |
| 61 | + slcD := []string{"apple", "orange"} |
| 62 | + slcB, _ := json.Marshal(slcD) |
| 63 | + fmt.Println(reflect.TypeOf(slcB)) |
| 64 | + fmt.Println(string(slcB)) |
| 65 | + fmt.Println(slcB) |
| 66 | + /** map |
| 67 | + []uint8 |
| 68 | + {"apple":5,"orange":6} |
| 69 | + [123 34 97 112 112 108 101 34 58 53 44 34 111 114 97 110 103 101 34 58 54 125] |
| 70 | + */ |
| 71 | + mapD := map[string]int{"apple": 5, "orange": 6} |
| 72 | + mapB, _ := json.Marshal(mapD) |
| 73 | + fmt.Println(reflect.TypeOf(mapB)) |
| 74 | + fmt.Println(string(mapB)) |
| 75 | + fmt.Println(mapB) |
| 76 | + |
| 77 | + //encoding automatically your custom data types. |
| 78 | + //{"Page":1,"Fruits":["apple","orange"]} |
| 79 | + res1D := &Response1{ |
| 80 | + Page: 1, |
| 81 | + Fruits: []string{"apple", "orange"}, |
| 82 | + } |
| 83 | + res1B, _ := json.Marshal(res1D) |
| 84 | + fmt.Println(string(res1B)) |
| 85 | + //use tags on struct field declarations to customize the encoded JSON key names. |
| 86 | + //{"page":1,"fruits":["apple","orange"]} [add tags to keys] |
| 87 | + res2D := &Response2{ |
| 88 | + Page: 1, |
| 89 | + Fruits: []string{"apple", "orange"}, |
| 90 | + } |
| 91 | + res2B, _ := json.Marshal(res2D) |
| 92 | + fmt.Println(string(res2B)) |
| 93 | + |
| 94 | + fmt.Println("DECODING JSON") |
| 95 | + /***************************DECODING JSON**************************/ |
| 96 | + /** |
| 97 | + 1. provide a variable where the JSON package can put the decoded data. |
| 98 | + 2. map[string]interface()/ will hold/ a map of/ strings to arbitray data types. |
| 99 | + */ |
| 100 | + var dat map[string]interface{} |
| 101 | + fmt.Println(reflect.TypeOf(dat)) //map[string]interface {} |
| 102 | + |
| 103 | + byt := []byte(`{"num":6.13,"strs":["apple","orange"]}`) |
| 104 | + //check for associated errors |
| 105 | + /** |
| 106 | + map[num:6.13 strs:[a b]] |
| 107 | + map[string]interface {} |
| 108 | + */ |
| 109 | + if err := json.Unmarshal(byt, &dat); err != nil { |
| 110 | + panic(err) |
| 111 | + } |
| 112 | + fmt.Println(dat) //map[num:6.13 strs:[a b]] |
| 113 | + fmt.Println(reflect.TypeOf(dat)) //map[string]interface {} |
| 114 | + |
| 115 | + // situation1: none-nested. get value to get directly. |
| 116 | + num := dat["num"].(float64) |
| 117 | + fmt.Println(num) //6.13 |
| 118 | + fmt.Println(reflect.TypeOf(num)) //float64 |
| 119 | + // situation2: nested. requires a series of casts. |
| 120 | + strs := dat["strs"].([]interface{}) |
| 121 | + str1 := strs[0].(string) |
| 122 | + fmt.Println(reflect.TypeOf(dat["strs"])) //[]interface {} |
| 123 | + fmt.Println(reflect.TypeOf(strs)) //[]interface {} |
| 124 | + fmt.Println(reflect.TypeOf(str1)) //string |
| 125 | + fmt.Println(str1) |
| 126 | + |
| 127 | + // decode JSON into custom data types. |
| 128 | + str := `{"page": 1, "fruits": ["apple", "peach"]}` |
| 129 | + res := Response2{} |
| 130 | + json.Unmarshal([]byte(str), &res) |
| 131 | + fmt.Println(res) //{1 [apple peach]} |
| 132 | + fmt.Println(res.Page) //1 |
| 133 | + fmt.Println(res.Fruits[0]) //apple |
| 134 | + |
| 135 | + /** |
| 136 | + Except used bytes and strings as imtermediates between the data and JSON represation on standard out above. |
| 137 | + We can also stream JSON encoding to os.Writer like os.Stdout or HTTP response bodies. |
| 138 | + */ |
| 139 | + enc := json.NewEncoder(os.Stdout) |
| 140 | + d := map[string]int{"apple": 5, "orange": 7} |
| 141 | + enc.Encode(d) //{"apple":5,"orange":7} |
| 142 | +} |
0 commit comments