1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
package main
import (
"encoding/json"
"fmt"
)
func main() {
// map里面
map1 := map[string]string {
"name":"张三",
"age":"18",
"home":"北京",
}
// 将map解析成json
json1, err := json.Marshal(map1)
if err != nil {
fmt.Println(err.Error())
}
fmt.Printf("json1: %s 类型: %T \n",json1,json1)
map2 := map[string][]string {
"fruit": {"香蕉","葡萄"},
"coder":{"PHP","Go","Java"},
"likes":{"打游戏","看动漫"},
}
json2, err := json.Marshal(map2)
if err != nil {
fmt.Println(err.Error())
}
fmt.Printf("json2: %s 类型: %T \n",json2,json2)
}
/**输出
json1: {"age":"18","home":"北京","name":"张三"} 类型: []uint8
json2: {"coder":["PHP","Go","Java"],"fruit":["香蕉","葡萄"],"likes":["打游戏","看动漫"]} 类型: []uint8
*/
|