如果json是map結(jié)構(gòu)
strJson
{
"總就業(yè)人數(shù)": 1375.66,
"第一產(chǎn)業(yè)": 40.83,
"第二產(chǎn)業(yè)": 422.82,
"省代碼": 310000.0,
"省": "上海市",
"市代碼": 310000.0,
"市": "上海市",
"類型": "直轄市"
}
var f interface{}
json.Unmarshal([]byte(strJson), &f)
if reflect.TypeOf(f).Kind() == reflect.Map {
m := reflect.ValueOf(f).Interface().(map[string]interface{})
fmt.Println(m)
}
運(yùn)行結(jié)果:
在這里插入圖片描述
如果json是slice結(jié)構(gòu)
strJson
[{
"showname": "總就業(yè)人數(shù)",
"name": "總就業(yè)人數(shù)",
"class": "float"
}, {
"showname": "第一產(chǎn)業(yè)",
"name": "第一產(chǎn)業(yè)",
"class": "float"
}, {
"showname": "第二產(chǎn)業(yè)",
"name": "第二產(chǎn)業(yè)",
"class": "float"
}, {
"showname": "省代碼",
"name": "省代碼",
"class": "float"
}, {
"showname": "省",
"name": "省",
"class": "text"
}, {
"showname": "市代碼",
"name": "市代碼",
"class": "float"
}, {
"showname": "市",
"name": "市",
"class": "text"
}, {
"showname": "類型",
"name": "類型",
"class": "text"
}]
var f interface{}
json.Unmarshal([]byte(str), &f)
if reflect.TypeOf(f).Kind() == reflect.Slice {
s := reflect.ValueOf(f)
for i := 0; i < s.Len(); i++ {
ele := s.Index(i).Interface().(map[string]interface{})
fmt.Println(ele)
}
}
運(yùn)行結(jié)果:
在這里插入圖片描述