【发布时间】:2013-12-04 19:12:53
【问题描述】:
我正在尝试解组一些 json,以便嵌套对象不会被解析,而只是被视为 string 或 []byte。
所以我想得到以下内容:
{
"id" : 15,
"foo" : { "foo": 123, "bar": "baz" }
}
解组为:
type Bar struct {
ID int64 `json:"id"`
Foo []byte `json:"foo"`
}
我收到以下错误:
json: cannot unmarshal object into Go value of type []uint8
【问题讨论】:
-
为什么不使用
map[string]interface{}?它还具有以正确方式重新编组的优点。 -
@JamesHolmes 通常不建议这样做,因为这允许任何类型,如果您不明确希望支持所有类型,请不要使用空接口(接口{}),它会导致您比它解决的问题更多
标签: json go unmarshalling