0
对象类型解码JSON
如果您有以下JSON结构:如何基于在Golang
[
{
"type": "home",
"name": "house #1",
... some number of properties for home #1
},
{
"type": "bike",
"name": "trek bike #1",
... some number of properties for bike #1
},
{
"type": "home",
"name": "house #2",
... some number of properties for home #2
}
]
你怎么不知道每种类型是什么,直到你解组对象Golang解码这一个结构。看起来你需要两次解组。
另外从我可以告诉,我应该使用RawMessage来延迟解码。但我不确定这看起来如何。
说我有以下结构:
type HomeType struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Bathrooms string `json:"bathrooms,omitempty"`
... more properties that are unique to a home
}
type BikeType struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Tires string `json:"tires,omitempty"`
... more properties that are unique to a bike
}
第二个问题。是否可以在流模式下执行此操作?对于这个数组真的很大?
感谢
如果有一个“类型”字段,并且所有属性都是字符串,为什么不直接解组到'[]地图[字符串]字符串'? – JimB
想象一下JSON对象有许多属性,包括子对象和对象数组。 – jordan2175