在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)
//}
}
登錄后復制
如果 foo1 不為空,則預期輸出:
Method is GET desc is It may be possible.
登錄后復制
解決方法
您可以將其解組到 map[string]interface{} 變量中,然后使用一系列類型斷言來獲取您想要的信息,例如:
var parseddata = make(map[string]interface{})
json.unmarshal([]byte(bjson), &parseddata)
fmt.printf("method is %s\n", parseddata["classifications"].
(map[string]interface{})["material"].
(map[string]interface{})["foo1"].
([]interface{})[0].
(map[string]interface{})["method"].(string))
登錄后復制
以上將輸出:
method is get
登錄后復制
這是完整的、可運行的代碼版本:
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]interface{})
json.unmarshal([]byte(bjson), &parseddata)
fmt.printf("method is %s\n", parseddata["classifications"].(map[string]interface{})["material"].(map[string]interface{})["foo1"].([]interface{})[0].(map[string]interface{})["method"].(string))
}
登錄后復制
如果我構建這個:
go build -o example main.go
登錄后復制
它的運行方式如下:
$ ./main method is get
登錄后復制
檢查值是否不存在或者是否為空列表:
data := parsedData["classifications"].(map[string]interface{})["Material"].(map[string]interface{})
val, ok := data["foo2"]
if !ok {
panic("no key foo2 in map")
}
if count := len(val.([]interface{})); count == 0 {
fmt.Printf("foo2 is empty\n")
} else {
fmt.Printf("foo2 has %d items", count)
}
登錄后復制






