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 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作