英文:
How to decode JSON based on object type in Golang
问题
如果你有以下的JSON结构:
[
{
"type": "home",
"name": "house #1",
...一些关于home #1的属性
},
{
"type": "bike",
"name": "trek bike #1",
...一些关于bike #1的属性
},
{
"type": "home",
"name": "house #2",
...一些关于home #2的属性
}
]
在Golang中,如果在解组对象之前不知道每个类型是什么,你如何将其解码为一个结构体。似乎你需要进行两次解组。
根据我所了解,你可能应该使用RawMessage来延迟解码。但我不确定具体的实现方式。
假设我有以下结构体:
type HomeType struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Bathrooms string `json:"bathrooms,omitempty"`
...更多home特有的属性
}
type BikeType struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Tires string `json:"tires,omitempty"`
...更多bike特有的属性
}
第二个问题。在这个数组非常大的情况下,是否可以以流式模式进行解码?
谢谢。
英文:
If you have the following JSON structure:
[
{
"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
}
]
How do you decode this in Golang to a struct without knowing what each type is until you unmarshall the object. It seems like you would have to do this unmarshalling twice.
Also from what I can tell, I should probably be using the RawMessage to delay the decoding. But I am not sure how this would look.
Say I had the following structs:
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
}
Second question. Is it possible to do this in streaming mode? For when this array is really large?
Thanks
答案1
得分: 2
如果你想操作对象,你需要知道它们的类型。但是如果你只是想传递它们,例如:如果你从数据库中获取一个大对象,只是为了将其编组(Marshal)并传递给客户端,你可以使用一个空的interface{}
类型:
type HomeType struct {
Name interface{} `json:"name,omitempty"`
Description interface{} `json:"description,omitempty"`
Bathrooms interface{} `json:"bathrooms,omitempty"`
// ...更多与房屋相关的属性
}
type BikeType struct {
Name interface{} `json:"name,omitempty"`
Description interface{} `json:"description,omitempty"`
Tires interface{} `json:"tires,omitempty"`
// ...更多与自行车相关的属性
}
点击链接了解更多关于空接口的信息。
希望这是你想要的。
英文:
If you want to manipulate the objects you will have to know what type they are.
But if you only want to pass them over, for example:
If you get from DB some big object only to Marshal it and pass it to client side,
you can use an empty interface{}
type:
type HomeType struct {
Name interface{} `json:"name,omitempty"`
Description interface{} `json:"description,omitempty"`
Bathrooms interface{} `json:"bathrooms,omitempty"`
... more properties that are unique to a home
}
type BikeType struct {
Name interface{} `json:"name,omitempty"`
Description interface{} `json:"description,omitempty"`
Tires interface{} `json:"tires,omitempty"`
... more properties that are unique to a bike
}
Read here for more about empty interfaces - link
Hope this is what you ment
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论