【发布时间】:2014-08-08 07:03:56
【问题描述】:
我正在调用一个 API,它将像这样返回 Json 对象:
{
name: "XXX"
type: "TYPE_1"
shared_fields: {...}
type_1_fields: {...}
..
type_2_fields: {...}
}
根据不同的类型,这个对象会有不同种类的字段,但是这些字段对于不同的类型是确定的。 因此,我将 Json 字符串解组为 map[string]interface{} 以获取不同的类型,但是如何将这些 map[string]interface{} 转换为某个结构?
var f map[string]interface{}
err := json.Unmarshal(b, &f)
type := f["type"]
switch type {
case "type_1":
//initialize struct of type_1
case "type_2":
//initialize struct of type_2
}
【问题讨论】: