如何解析嵌套 JSON 对象中的内部字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13593519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to parse an inner field in a nested JSON object
提问by keks
I have a JSON object similar to this one:
我有一个类似于这个的 JSON 对象:
{
"name": "Cain",
"parents": {
"mother" : "Eve",
"father" : "Adam"
}
}
Now I want to parse "name" and "mother" into this struct:
现在我想将“name”和“mother”解析到这个结构中:
struct {
Name String
Mother String `json:"???"`
}
I want to specify the JSON field name with the json:...struct tag, however I don't know what to use as tag, because it is not the top object I am interested in. I found nothing about this in the encoding/jsonpackage docs nor in the popular blog post JSON and Go. I also tested mother, parents/motherand parents.mother.
我想用json:...struct 标签指定 JSON 字段名称,但是我不知道使用什么作为标签,因为它不是我感兴趣的顶级对象。我在encoding/json包文档和流行的博客文章JSON 和 Go。我也测试过mother,parents/mother和parents.mother。
采纳答案by dskinner
Unfortunately, unlike encoding/xml, the jsonpackage doesn't provide a way to access nested values. You'll want to either create a separate Parents struct or assign the type to be map[string]string. For example:
不幸的是,与 不同encoding/xml,该json包不提供访问嵌套值的方法。您需要创建一个单独的Parents 结构或将类型分配为map[string]string. 例如:
type Person struct {
Name string
Parents map[string]string
}
You could then provide a getter for mother as so:
然后你可以为母亲提供一个吸气剂:
func (p *Person) Mother() string {
return p.Parents["mother"]
}
This may or may not play into your current codebase and if refactoring the Motherfield to a method call is not on the menu, then you may want to create a separate method for decoding and conforming to your current struct.
这可能会或可能不会在您当前的代码库中发挥作用,并且如果将Mother字段重构为方法调用不在菜单上,那么您可能需要创建一个单独的方法来解码并符合您当前的结构。
回答by Daniel
You could use structs so long as your incoming data isn't too dynamic.
只要传入的数据不是太动态,就可以使用结构。
http://play.golang.org/p/bUZ8l6WgvL
http://play.golang.org/p/bUZ8l6WgvL
package main
import (
"fmt"
"encoding/json"
)
type User struct {
Name string
Parents struct {
Mother string
Father string
}
}
func main() {
encoded := `{
"name": "Cain",
"parents": {
"mother": "Eve",
"father": "Adam"
}
}`
// Decode the json object
u := &User{}
err := json.Unmarshal([]byte(encoded), &u)
if err != nil {
panic(err)
}
// Print out mother and father
fmt.Printf("Mother: %s\n", u.Parents.Mother)
fmt.Printf("Father: %s\n", u.Parents.Father)
}
回答by Daniel
Here's some code I baked up real quick in the Go Playground
这是我在 Go Playground 中快速编写的一些代码
http://play.golang.org/p/PiWwpUbBqt
http://play.golang.org/p/PiWwpUbBqt
package main
import (
"fmt"
"encoding/json"
)
func main() {
encoded := `{
"name": "Cain",
"parents": {
"mother": "Eve"
"father": "Adam"
}
}`
// Decode the json object
var j map[string]interface{}
err := json.Unmarshal([]byte(encoded), &j)
if err != nil {
panic(err)
}
// pull out the parents object
parents := j["parents"].(map[string]interface{})
// Print out mother and father
fmt.Printf("Mother: %s\n", parents["mother"].(string))
fmt.Printf("Father: %s\n", parents["father"].(string))
}
There might be a better way. I'm looking forward to seeing the other answers. :-)
可能有更好的方法。我期待看到其他答案。:-)
回答by changingrainbows
回答by tike
How about using an intermediary struct as the one suggested above for parsing, and then putting the relevant values in your "real" struct?
如何使用中间结构作为上面建议的解析结构,然后将相关值放入“真实”结构中?
import (
"fmt"
"encoding/json"
)
type MyObject struct{
Name string
Mother string
}
type MyParseObj struct{
Name string
Parents struct {
Mother string
Father string
}
}
func main() {
encoded := `{
"name": "Cain",
"parents": {
"mother": "Eve",
"father": "Adam"
}
}`
pj := &MyParseObj{}
if err := json.Unmarshal([]byte(encoded), pj); err != nil {
return
}
final := &MyObject{Name: pj.Name, Mother: pj.Parents.Mother}
fmt.Println(final)
}

