登录
首页 >  Golang >  Go问答

在 Go 语言中处理深度嵌套的 JSON 数据

来源:stackoverflow

时间:2024-03-10 21:03:27 338浏览 收藏

对于一个Golang开发者来说,牢固扎实的基础是十分重要的,golang学习网就来带大家一点点的掌握基础知识点。今天本篇文章带大家了解《在 Go 语言中处理深度嵌套的 JSON 数据》,主要介绍了,希望对大家的知识积累有所帮助,快点收藏起来吧,否则需要时就找不到了!

问题内容

我有一个我认为非常混乱的 json 块,我想阅读并 使用 go 修改两个深度嵌套的值(表示:我想要这个!)。 由于我将其发送到的服务器, 我无法更改标签名称。是什么让它特别困难 对我来说,父母有多个嵌套的孩子,并且由于有太多“值”标签,我不知道如何指定要输入哪个“值”孩子。

我使用这个很快就得到了 bash 中的值

jq ' .value[0].value[1].value[0].value[1].value[0].value="'"$one"'" | '\ '
.value[0].value[1].value[0].value[1].value[1].value="'"$two"'"'

我最初在 go 中尝试了这样的格式,但由于子项都被命名为“value”的问题而无法使其工作,而我想进入第一个以外的其他格式。不幸的是,这些神奇的 json to go 结构都无法处理所有嵌套。

type bar struct {
    value struct { // value[0]
        value struct {  // value[1]
            value struct { // value[0]
                value struct { // value[1]
                    value struct { // value[1] or value[2]
                    }}}}}}

此代码将 json 转换为更结构/映射友好的形式,并打印整个内容。

var foo interface{}
err := json.unmarshal(blob, &foo)
if err != nil {
    fmt.println("error:", err)
}

m := foo.(map[string]interface{})

// print all
fmt.println("loop: ", m)
for k, v := range m {
    fmt.printf("%v : %v", k, v)
}

这是我存储为变量的 json

var blob = []byte(`{
"tag": "1",
"value": [{
        "tag": "2",
        "value": [{
                "tag": "3",
                "value": [{
                        "tag": "4",
                        "type": "x",
                        "value": "x"
                    }, {
                        "tag": "5",
                        "type": "x",
                        "value": "x"
                    }
                ]
            }, {
                "tag": "6",
                "value": [{
                        "tag": "7",
                        "value": [{
                                "tag": "8",
                                "type": "x",
                                "value": "x"
                            }, {
                                "tag": "9",
                                "value": [{
                                        "tag": "10",
                                        "type": "x",
                                        "value": "I want this!"
                                    }, {
                                        "tag": "11",
                                        "type": "Thanks for the help mate!",
                                        "value": "I want this!"
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }, {
                "tag": "12",
                "type": "x",
                "value": "x"
            }
        ]
    }, {
        "tag": "13",
        "value": [{
                "tag": "14",
                "type": "x",
                "value": "x"
            }, {
                "tag": "15",
                "value": [{
                        "tag": "16",
                        "value": [{
                                "tag": "17",
                                "type": "x",
                                "value": "x"
                            }
                        ]
                    }
                ]
            }
        ]
    }
]
}`)

如果您能给我任何帮助或建议,我将不胜感激。


解决方案


您最初的尝试没有成功,因为您使用了需要结构体切片的结构体 (value []struct{...})。但这也不起作用,因为在某些情况下,值是切片,在某些情况下它们是字符串,这是 encoding/json 不喜欢的。您最好的选择是两个选项之一:原始文本操作,这可能会更快,但更脆弱且容易出错,或者像第二个示例中那样解码为 map[string]interface{} 并执行一些粗糙的类型断言:

var foo map[string]interface{}
err := json.Unmarshal(blob, &foo)
if err != nil {
    fmt.Println("error:", err)
}

foo["value"].([]interface{})[0].(map[string]interface{})["value"].([]interface{})[1].(map[string]interface{})["value"].([]interface{})[0].(map[string]interface{})["value"].([]interface{})[1].(map[string]interface{})["value"].([]interface{})[0].(map[string]interface{})["value"]

今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>