首頁 >後端開發 >Golang >在 Go Lang 中處理巢狀非結構化 JSON

在 Go Lang 中處理巢狀非結構化 JSON

WBOY
WBOY轉載
2024-02-11 22:36:091035瀏覽

在 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)
    //}
}

如果 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)
}

以上是在 Go Lang 中處理巢狀非結構化 JSON的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除