dragonmeng2002
2012-11-27 21:36
浏览 872
已采纳

如何在Golang中解析嵌套JSON对象中的内部字段?

I have a JSON object similar to this one:

{
  "name": "Cain",
  "parents": {
    "mother" : "Eve",
    "father" : "Adam"
  }
}

Now I want to parse "name" and "mother" into this struct:

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/json package docs nor in the popular blog post JSON and Go. I also tested mother, parents/mother and parents.mother.

图片转代码服务由CSDN问答提供 功能建议

我有一个与此对象相似的JSON对象:</ p>

   {
“ name”:“ Cain”,
“ parents”:{
“ mother”:“ Eve”,
“ father”:“ Adam” 
} 
} 
 </ code> <  / pre> 
 
 

现在我想将“ name”和“ mother”解析为该结构:</ p>

  struct {
 Name String 
 Mother String  json:“ ???”`
} 
 </ code> </ pre> 
 
 

我想用 json:... </ code指定JSON字段名称 > struct标记,但是我不知道该用什么,因为它不是我感兴趣的顶级对象。在 encoding / json </ code> 包文档或热门博客文章 JSON和Go 。 我还测试了 mother </ code>, parents / mother </ code>和 parents.mother </ code>。</ p> </ div>

5条回答 默认 最新

相关推荐 更多相似问题