douyao1856
2017-03-21 23:21
浏览 164

golang解析json文件

I want to parse the following json file and also reference the individual fields.

The json file has some known pattern: The json file is has two group levels. It will have a variable but unknown number of first level groups. Each first level group will have two second level groups: the rule and config groups. Both the rule group and the config group will have unknown number of key:value pairs.

{
  { // Comment: first level group
    { // Comment: second level group
      "rule1": "doA"
      "rule2": "doB"
      "rule3": "doC"
      ...
    }
    {
      "config1": "goA"
      "configX": "goB"
      ...
    }
  }
  ...
  {
    {
      "rule100": "doAAA"
      "rule200": "doBBB"
      "rule300": "doCCC"
      ...
    }
    {
      "config100": "goAAA"
      "configX00": "goBBB"
      ...
  }
}

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

我想解析以下json文件,并引用各个字段。

json文件具有一些已知的模式:json文件具有两个组级别。 它将具有可变的但未知数量的第一级组。 每个第一级组将具有两个第二级组:规则组和配置组。

  {
 {//注释:第一级组
 {//注释: 第二层组
“ rule1”:“ doA” 
“ rule2”:“ doB” 
“ rule3”:“ doC” 
 ... 
} 
 {
“ config1”:“ goA”  
“ configX”:“ goB” 
 ... 
} 
} 
 ... 
 {
 {
“ rule100”:“ doAAA” 
“ rule200”:“ doBBB” \  n“ rule300”:“ doCCC” 
 ... 
} 
 {
“ config100”:“ goAAA” 
“ configX00”:“ goBBB” 
 ... 
} 
} 
    
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dousi4950 2017-03-21 23:37
    已采纳

    You can use maps and slices for those unknown numbers.

    type FirstGroup struct {
        Rules Rules `json:"rules"`
        Configs Configs `json:"configs"`
    }
    
    type Rules map[string]string
    
    type Configs map[string]string
    

    https://play.golang.org/p/zCymz62B9K <- The json in this example is a modified version of your's because your's is not really json.

    打赏 评论

相关推荐 更多相似问题