duanmao9918 2018-02-01 11:43
浏览 52

如何创建无限嵌套的json并在go lang数据结构中访问相同的JSON?

How to create a infinite nested json and access the same in go lang data structures,

For example below is the sample json with 3 levels, in general it should be dynamic, user can add any children by selecting the value from the dropdown which is populated tree like dropdown on UI.

{
        "value": {
            "Id": "1",
            "Text": "abcd",
            "Parent": ""
        },
        "children": [
            {
                "value": {
                    "Id": "2",
                    "Text": "abcd",
                    "Parent": "1"
                },
                "children": [
                    {
                        "value": {
                            "Id": "3",
                            "Text": "abcd",
                            "Parent": "1"
                        }
                    }
                ]
            }
        ]
    }

structures in go: I have created this go data structure but it will access only upto 3 levels based on the above json, can we make this data structure as dynamic where it should handle infinite nested json.

 type AutoGenerated struct {
        Value struct {
            ID     string `json:"Id"`
            Text   string `json:"Text"`
            Parent string `json:"Parent"`
        } `json:"value"`
        Children []struct {
            Value struct {
                ID     string `json:"Id"`
                Text   string `json:"Text"`
                Parent string `json:"Parent"`
            } `json:"value"`
            Children []struct {
                Value struct {
                    ID     string `json:"Id"`
                    Text   string `json:"Text"`
                    Parent string `json:"Parent"`
                } `json:"value"`
            } `json:"children"`
        } `json:"children"`
    }

Note: we can add n number of parents and n number of children, is this possible in Go data structures?

can you suggest best and easy way to implement this?

How can we add/delete/edit any parent or child? (The above json example will come from UI) ? To add/delete/edit any parent or child which json structure or id needed?

  • 写回答

3条回答 默认 最新

  • dongman2721 2018-02-01 11:50
    关注

    You can use recursive structures in Go to represent this json (by recursive, I mean that Level contains a []Level):

    type Value struct {
        ID     string
        Text   string
        Parent string
    }
    
    type Level struct {
        Value    Value `json:"value"`
        Children []Level
    }
    

    Given the json you listed as the string j, I can now unmarshal it as follows:

    var root Level
    err := json.Unmarshal([]byte(j), &root)
    if err != nil {
        panic(err)
    }
    fmt.Println(root)
    fmt.Println(root.Children[0])
    fmt.Println(root.Children[0].Children[0])
    

    This outputs:

    {{1 abcd } [{{2 abcd 1} [{{3 abcd 1} []}]}]}
    {{2 abcd 1} [{{3 abcd 1} []}]}
    {{3 abcd 1} []}
    

    Go playground link

    评论

报告相同问题?

悬赏问题

  • ¥15 Fluent udf 编写问题
  • ¥15 求合并两个字节流VB6代码
  • ¥15 Pyqt 如何正确的关掉Qthread,并且释放其中的锁?
  • ¥30 网站服务器通过node.js部署了一个项目!前端访问失败
  • ¥15 WPS访问权限不足怎么解决
  • ¥15 java幂等控制问题
  • ¥15 海湾GST-DJ-N500
  • ¥15 氧化掩蔽层与注入条件关系
  • ¥15 Django DRF 如何反序列化得到Python对象类型数据
  • ¥15 多数据源与Hystrix的冲突