go语言解析 map[string]interface{} 数据格式的注意事项:
- map记得分配内存
- 解析出来的int类型会变成float64类型
- 注意判断不为nil后再转换类型
示例代码:
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
package main
import (
"fmt"
"encoding/json"
)
func main() {
var m map[string]interface{} //声明变量,不分配内存
m = make(map[string]interface{}) //必可不少,分配内存
m["name"] = "saul"
var age int = 20
m["age"] = age
m["nation"] = "China"
print_map(m)
fmt.Println("==============")
data, err:=json.Marshal(m)
if err != nil {
}
fmt.Println("err:", err)
fmt.Println(data)
fmt.Println()
m1 := make(map[string]interface{})
err = json.Unmarshal(data, &m1)
fmt.Println("err:", err)
fmt.Println(m1)
print_map(m1)
fmt.Println()
if m1["name"]!= nil {
fmt.Println(m1["name"].(string))
}
if m1["type"]!= nil {
fmt.Println(m1["type"].(string))
} else {
fmt.Println("there is not the key of type")
}
}
//解析 map[string]interface{} 数据格式
func print_map(m map[string]interface{}) {
for k, v := range m {
switch value := v.(type) {
case nil:
fmt.Println(k, "is nil", "null")
case string:
fmt.Println(k, "is string", value)
case int:
fmt.Println(k, "is int", value)
case float64:
fmt.Println(k, "is float64", value)
case []interface{}:
fmt.Println(k, "is an array:")
for i, u := range value {
fmt.Println(i, u)
}
case map[string]interface{}:
fmt.Println(k, "is an map:")
print_map(value)
default:
fmt.Println(k, "is unknown type", fmt.Sprintf("%T", v))
}
}
}
|
结果:
1
2
3
4
5
6
7
8
9
10
11
12
|
name is string saul
age is int 20
nation is string China
==============
[123 34 97 103 101 34 58 50 48 44 34 110 97 109 101 34 58 34 115 97 117 108 34 44 34 110 97 116 105 111 110 34 58 34 67 104 105 110 97 34 125]
==============
age is float64 20
name is string saul
nation is string China
==============
saul
there is not the key of type
|