我已经阅读了Unmarshaling嵌套的JSON对象,但是我仍然不确定如何处理json:
{
"input":{
"lat":1234,
"lon":1234
},
"stuff":[
{
"soc":"510950802051011",
"bbox":[
-76.743917,
37.298812,
-76.741184,
37.300357
],
"ccn":"51095",
"name":"James",
"age":"51",
"gf":"Mary",
"state":"NYC",
"pea":"PEA033",
"rea":"REA002",
"rpc":"RPC002",
"vpc":"VPC002"
}
]
}
我只想访问stuff.ccn,stuff.name
package main
import (
"encoding/json"
"fmt"
)
func main() {
jStr := `{
"input":{
"lat":1234,
"lon":1234
},
"stuff":[
{
"soc":"510950802051011",
"bbox":[
-76.743917,
37.298812,
-76.741184,
37.300357
],
"ccn":"51095",
"name":"James",
"age":"51",
"gf":"Mary",
"state":"NYC",
"pea":"PEA033",
"rea":"REA002",
"rpc":"RPC002",
"vpc":"VPC002"
}
]
}`
type Inner struct {
Key2 []string `json:"ccn"`
Key3 []string `json:"name"`
}
type Outer struct {
Key Inner `json:"stuff"`
}
var cont Outer
json.Unmarshal([]byte(jStr), &cont)
fmt.Printf("%+v\n", cont)
}
我认为我遇到的问题与数组有关。
您的结构需要遵循JSON中的内容。在提供jStr
顶级对象映射的地方,Outer
您似乎拥有一个键stuff
,它是Inner
对象的数组。您需要修改您的类型以反映出这种情况:
type Inner struct {
Key2 string `json:"ccn"`
Key3 string `json:"name"`
}
type Outer struct {
Key []Inner `json:"stuff"`
}
这基本上是说,stuff
找到后将其作为数组,将每个项目解组Inner
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句