duannao8450
2018-06-29 23:37
浏览 356

如何在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?

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

我有一个看起来像这样的json文件

  {  
“链接”:{
“ uniqueurl_1”:{
“ a”:{
“ b”:[
“东西” 
],
“ I”:{
“想要”:  {
“ to”:“ morestuff”,
“ go”:{
“ in”:{
“ here”:{
“ and”:“ array”,
“ itis”:{\  n“非常”:“字符串”,
“深”:“ stringIwant” 
} 
} 
} 
} 
} 
} 
} 
} 
} 
} 
    
 
 

,我想获取 uniqueurl_1 (每个链接总是不同的),并且我还想获​​取“深”字段的 值(“ stringIwant”)

我知道它嵌套的不是很深,您可以在Golang中创建一堆结构,但是当深度很深时,它仍然是唯一/最好的 这样做吗?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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"`
    }
    
    点赞 评论
  • doushou8730 2018-06-30 10:03

    For this case you can use gojsonq - A simple Go package to Query over JSON Data library. Here is full list of available queries.

    So if you want get the value (stringIwant), you can choose or method From or method Find.

    Example below:

    package main
    
    import (
        "fmt"
    
        "github.com/thedevsaddam/gojsonq"
    )
    
    const json = `{
      "links": {
        "uniqueurl_1": {
          "a": {
            "b": [
              "stuff"
            ],
            "I": {
              "want": {
                "to": "morestuff",
                "go": {
                  "in": {
                    "here": {
                      "and": "array",
                      "itis": {
                        "very": "string",
                        "deep": "stringIwant"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }`
    
    func main() {
        key := "uniqueurl_1"
        query := "links." + key + ".a.I.want.go.in.here.itis.deep"
        fmt.Println(gojsonq.New().JSONString(json).From(query).Get())
        fmt.Println(gojsonq.New().JSONString(json).Find(query).(string))
    }
    

    Output:

    stringIwant

    stringIwant

    点赞 评论

相关推荐 更多相似问题