英文:
Json unmarshal type model in golang
问题
我有一个返回类似下面的响应的 RESTful 服务:
"Basket" : {
"Count": 1,
"Fruits": [
{
"Name":"Mango",
"Season":"Summer"
},
{
"Name":"Fig",
"Season":"Winter"
}
]
}
我正在尝试创建一个 Go 语言模型来解组内容。以下是我尝试过的代码:
type Response struct {
Count int
Fruits []Fruit
}
type Fruit struct {
Name string
Season string
}
但是,当我在测试代码中对 Response 对象进行编组时,我没有看到类似的 JSON 结构。(https://play.golang.org/p/EGKqfbwFvW)
编组后的数据总是出现如下形式:
{
"Count":100,
"Fruits":[
{"Name":"Mango","Season":"Summer"},
{"Name":"Fig","Season":"Winter"}
]
}
请注意,原始 JSON 中的 Fruits 是一个数组 [],而不是 {[]}。我该如何在 golang 中为这个响应建模?
英文:
I have a RESTful service that returns response similar to show below:
"Basket" : {
"Count": 1,
"Fruits": {[
{
"Name":"Mango",
"Season":"Summer"
},
{
"Name":"Fig",
"Season":"Winter"}
]}
}
I am trying to create Go lang model to unmarshal the contents. Following is the code I have tried:
type Response struct {
Count int
Fruits []Fruit
}
type Fruit struct {
Name string
Season string
}
But when I marshal the Response object in my test code I don't see similar json. (https://play.golang.org/p/EGKqfbwFvW)
Marshalled data always appears as :
{
"Count":100,
"Fruits":[
{"Name":"Mango","Season":"Summer"},
{"Name":"Fig","Season":"Winter"}
]
}
Notice the Fruits appearing as array [] and not {[]} in original json. How can I model structs in golang for this response?
答案1
得分: 2
你的模型是完全正确和有效的,但是 JSON 对象不正确。"Fruits"
没有名称,如果它应该是键值对,或者它应该被包裹在[]
而不是{}
中。
JSON 对象应该像这样格式化:
{
"Basket" : {
"Count": 1,
"Fruits": [
{
"Name":"Mango",
"Season":"Summer"
},
{
"Name":"Fig",
"Season":"Winter"
}
]
}
}
实际上,无效的 JSON 不应该起作用 链接。
英文:
Your model is totally correct and valid, but the JSON object is not. "Fruits"
doesn't have name if it should be key value pair or it should be wrapped in []
not {}
.
JSON obj should be formatted like this:
{
"Basket" : {
"Count": 1,
"Fruits": [
{
"Name":"Mango",
"Season":"Summer"
},
{
"Name":"Fig",
"Season":"Winter"
}
]
}
}
And actually invalid json shouldn't work https://play.golang.org/p/yoW7t4NfI7
答案2
得分: 1
我会将'Baskets'作为'Response'中的一个结构体,并创建一个'BasketsData'结构体,并为其添加一些标签。
type Fruit struct {
Name string `json:"Name"`
Season string `json:"Season"`
}
type BasketData struct {
Count int `json:"Count"`
Fruits []Fruit `json:"Fruits"`
}
type Response struct {
Basket BasketData `json:"Basket"`
}
这样,当你进行编组时,你将得到一个顶级的JSON响应。
fruitmania := []Fruit{{Name: "Mango", Season: "Summer"},
{Name: "Fig", Season: "Winter"}}
basket := Response{BasketData{Count: 100, Fruits: fruitmania}}
b, _ := json.Marshal(basket)
fmt.Println(string(b))
请查看以下链接以了解更多信息:
https://play.golang.org/p/TuUwBLs_Ql
英文:
I would make 'Baskets' a struct within 'Response', create a 'BasketsData' struct, and give it all some labels.
type Fruit struct {
Name string `json:"Name"`
Season string `json:"Season"`
}
type BasketData struct {
Count int `json:"Count"`
Fruits []Fruit `json:"Fruits"`
}
type Response struct {
Basket BasketData `json:"Basket"`
}
This way you will get a top level JSON response when you marshall it.
fruitmania := []Fruit{{Name: "Mango", Season: "Summer"},
{Name: "Fig", Season: "Winter"}}
basket := Response{BasketData{Count: 100, Fruits: fruitmania}}
b, _ := json.Marshal(basket)
fmt.Println(string(b))
checkit-checkit out:
https://play.golang.org/p/TuUwBLs_Ql
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论