duannao8450 2018-06-29 23:37
浏览 474
已采纳

如何在Golang中获取JSON唯一字段的名称和深度嵌套的子字段的值?

I have a json file that looks like this

{
  "links": {
    "uniqueurl_1": {
      "a": {
        "b": [
          "stuff"
        ],
        "I": {
          "want": {
            "to": "morestuff",
            "go": {
              "in": {
                "here": {
                  "and": "array",
                  "itis": {
                    "very": "string",
                    "deep": "stringIwant"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

And I want to get the uniqueurl_1 (that is always different for each link), and I also want to get the "deep" field's value ("stringIwant")

I know if it's not too deeply nested, you can just create a bunch of structs in Golang, but when it's this deep would it still be the only/best way to do it?

  • 写回答

2条回答 默认 最新

  • douchengchen7959 2018-06-29 23:57
    关注

    in your JSON is missing opening brace. Have a try in this way:

    {
      "links": {
        "uniqueurl_1": {
          "a": {
            "b": [
              "stuff"
            ],
            "I": {
              "want": {
                "to": "morestuff",
                "go": {
                  "in": {
                    "here": {
                      "and": "array",
                      "itis": {
                        "very": "string",
                        "deep": "stringIwant"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
    

    And use the following code to access you data:

    package main
    
    import "encoding/json"
    
    func UnmarshalNotSoDeep(data []byte) (NotSoDeep, error) {
        var r NotSoDeep
        err := json.Unmarshal(data, &r)
        return r, err
    }
    
    func (r *NotSoDeep) Marshal() ([]byte, error) {
        return json.Marshal(r)
    }
    
    type NotSoDeep struct {
        Links Links `json:"links"`
    }
    
    type Links struct {
        Uniqueurl1 Uniqueurl1 `json:"uniqueurl_1"`
    }
    
    type Uniqueurl1 struct {
        A A `json:"a"`
    }
    
    type A struct {
        B []string `json:"b"`
        I I        `json:"I"`
    }
    
    type I struct {
        Want Want `json:"want"`
    }
    
    type Want struct {
        To string `json:"to"`
        Go Go     `json:"go"`
    }
    
    type Go struct {
        In In `json:"in"`
    }
    
    type In struct {
        Here Here `json:"here"`
    }
    
    type Here struct {
        And  string `json:"and"` 
        Itis Itis   `json:"itis"`
    }
    
    type Itis struct {
        Very string `json:"very"`
        Deep string `json:"deep"`
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀