您将需要使用地图(类型为
map[string]interface{})来处理动态JSON。这是创建新地图的示例:// Initial declarationm := map[string]interface{}{ "key": "value",}// Dynamically add a sub-mapm["sub"] = map[string]interface{}{ "deepKey": "deepValue",}将JSON解组到地图中看起来像:
var f interface{}err := json.Unmarshal(b, &f)上面的代码将为您提供的地图
f,其结构类似于:
f = map[string]interface{}{ "Name": "Wednesday", "Age": 6, "Parents": []interface{}{ "Gomez", "Morticia", },}您将需要使用类型断言来访问它,否则Go不会知道它是一个映射:
m := f.(map[string]interface{})您还需要在从地图中拉出的每个项目上使用断言或键入开关。处理非结构化JSON很麻烦。
更多信息:
- https://blog.golang.org/json-and-go
- https://godoc.org/encoding/json#Unmarshal



