在 Go Lang 中处理嵌套非结构化 JSON
在Go Lang中处理嵌套非结构化JSON是一项关键任务。JSON(JavaScript Object Notation)是一种常用的数据交换格式,但当JSON数据嵌套复杂时,处理起来可能会变得困难。php小编鱼仔将为您介绍一些在Go Lang中处理嵌套非结构化JSON的方法和技巧,帮助您更高效地解析和操作这些数据。通过掌握这些技能,您将能够轻松处理复杂的JSON数据,提高代码的可读性和可维护性。
问题内容
我试图了解如何从 golang 中的非结构化 json 数据访问特定数据。我有以下 json,当 foo1 有一些与空的 foo2 不同的数据时,我尝试访问 material 下的“foo1”。当像 foo1 这样的对象有数据时,我还需要从同名的分类部分读取数据。例如。由于material部分下的foo1有数据,我应该已经打印material->foo1下的方法键值以及来自分类-> foo1的desc。
package main import ( "encoding/json" "fmt" ) type new struct { desc string `json:"desc"` } func main() { bjson := `{ "classifications": { "foo1": { "desc": "it may be possible.", "sol": "the backups.", "ref": { "sensitive information": "https://www.sensitive_information.html", "control sphere": "https://ww.example.org/data.html" },"bar1": { "desc": "the target", "sol": "should be used.", "ref": { "abc: srgery": "https://www.orp.org/" } }}, "material": { "backup file": [],"foo1": [ { "method": "get", "info": "it is not set", "level": 1, "parameter": "", "referer": "", "module": "diq", "curl_command": "curl "https://example.com/"", "wsg": [ "conf-12", "o-policy" ] }],"foo2": [],"bar1": []}, "anomalies": { "server error": [], "resource consumption": [] }, "additionals": { "web technology": [], "methods": [] }, "infos": { "url": "https://example.com/", "date": "thu, 08 dec 2022 06:52:04 +0000"}}}` var parseddata = make(map[string]map[string]new) json.unmarshal([]byte(bjson), &parseddata) fmt.println("output of parseddata - n", parseddata["classifications"]["foo1"].desc) //for _, v := range parseddata["material"] { // fmt.println(v) //} }登录后复制