亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

嵌套 JSON 中的 Golang Unmarshall 特定對象

嵌套 JSON 中的 Golang Unmarshall 特定對象

Go
慕的地8271018 2022-08-15 19:32:42
在 GO 中使用靜態屬性名稱取消對大型 JSON 的特定部分的重組的最佳做法是什么?我的 JSON 映射看起來像{    "top_view" : {       "@self" : "https://generic.com",           "graph" : {              "nodes" : [ { } ],              "relations" : [ { } ]                     },         "view_status" : {}                 }}我只需要檢索節點數組這是我到目前為止得到的,https://play.golang.org/p/btfRojEGqUu我只知道如何取消編組節點部分,但我不知道如何告訴Go僅開始解編該對象,因此如果我為整個JSON樹提供代碼,代碼將不起作用。請指教,謝謝!
查看完整描述

2 回答

?
長風秋雁

TA貢獻1757條經驗 獲得超7個贊

這是執行此操作的一種方法:定義一個顯示節點路徑的結構。您可以跳過 JSON 中不感興趣的成員。例如:


type Whole struct {

    TopView struct {

        Self  string `json:"@self"`

        Graph struct {

            Nodes []Node `json:"nodes"`

        } `json:"graph"`

    } `json:"top_view"`

}

然后封送出節點


var whole Whole


err := json.Unmarshal([]byte(jsonResp), &whole)

這是工作代碼:https://play.golang.org/p/5WvPocce_vh


查看完整回答
反對 回復 2022-08-15
?
LEATH

TA貢獻1936條經驗 獲得超7個贊

您可以取消編組任何 JSON 數據,而無需在代碼中定義任何具體類型。一個用途和向下鉆取的數據只是未分組。gotype assertiontype switch


以下是使用上述概念的問題的工作示例。您可能需要進一步完善此基礎,但基本概念保持不變:


package main


import (

    "encoding/json"

    "fmt"

    "log"

)


func main() {

    jsonResponse := `{

        "top_view" : {

           "@self" : "https://generic.com",

               "graph" : {

                  "nodes" : [ {

                    "@self" : "https://generic.com:443",

                    "id" : "1;;45d554600be28ee49c99d26e536225ea;;461ff354437881f6b5973d4af366b91c;;4c0f2cc8e29a4fe09240b2a0c311508d",

                    "ci" : {

                       "id" : "4c0f2cc8e29a4fe09240b2a0c311508d",

                       "name" : "NOICE",

                       "type" : "ci_collection",

                       "type_label" : "CiCollection",

                       "icon" : "/logical_group_32.svg"

                    },

                    "has_children" : true

                 }, {

                    "@self" : "https://generic.com:443hjkhk",

                    "id" : "1;;45d554600be28ee49c99d26e536225ea;;4e22910a478bf6939aed36fef22dc79e;;4a7788eeecbbeee3a8bb3189ba67f269",

                    "ci" : {

                       "id" : "4a7788eeecbbeee3a8bb3189ba67f269",

                       "name" : "NOTNOICE",

                       "type" : "ci_collection",

                       "type_label" : "CiCollection",

                       "icon" : "/logical_group_32.svg"

                    },

                    "has_children" : true

                 }, {

                    "@self" : "https://generic.com:443/fghfgh",

                    "id" : "1;;45d554600be28ee49c99d26e536225ea;;461ff354437881f6b5973d4af366b91c;;4c0f2cc8e29a4fe09240b2a0c311508d;;40b8b314821d01ac874f7209c228ab8f",

                    "ci" : {

                       "id" : "40b8b314821d01ac874f7209c228ab8f",

                       "name" : "NOICE",

                       "type" : "ci_collection",

                       "type_label" : "CiCollection",

                       "icon" : "/logical_group_32.svg"

                    },

                    "has_children" : true

                 }, {

                    "@self" : "https://generic.com:443/gfhgh",

                    "id" : "1;;45d554600be28ee49c99d26e536225ea;;4a208e0deee006668bb3cfab6541a869;;4bd30d48bc1c81b398e17435ba519f2d;;4a1c671cefd3b5b9931b4312382ff2e4",

                    "ci" : {

                       "id" : "4a1c671cefd3b5b9931b4312382ff2e4",

                       "name" : "AAA",

                       "type" : "ci_collection",

                       "type_label" : "CiCollection",

                       "icon" : "/logical_group_32.svg"

                    },

                    "has_children" : true

                 } ],

                  "relations" : [ { } ]

                         },

             "view_status" : {}

                     }

    }`


    var res interface{}

    err := json.Unmarshal([]byte(jsonResponse), &res)

    if err != nil {

        log.Fatal(err)

    }


    parseArbitoryJSONObject(res)

}


func parseArbitoryJSONObject(jsonObject interface{}) {

    data := jsonObject.(map[string]interface{})


    for k, v := range data {

        switch val := v.(type) {

        case string:

            fmt.Println(k, "is string:", val)

        case float64:

            fmt.Println(k, "is float64", val)

        case bool:

            fmt.Println(k, "is boolean", val)

        case []interface{}:

            fmt.Println(k, "is an array:")

            for i, u := range val {

                fmt.Println(i, u)

            }

        case map[string]interface{}:

            fmt.Println(k, "is an map[string]interface{}:")

            parseArbitoryJSONObject(val)

        default:

            fmt.Println(k, "is of a type I don't know how to handle")

        }

    }

}

在這里,可以使用可以保存任何值。函數用于促進從未分組的數據中提取價值。json.Unmarshalempty interfaceparseArbitoryJSONObject()type assertion


您可能需要進一步放置或修改邏輯,以便從未映射的 JSON 對象中獲取所需的項。type switch


以下是go-playground代碼片段鏈接供您參考:


https://play.golang.org/p/VUJsSXxSfVG


查看完整回答
反對 回復 2022-08-15
  • 2 回答
  • 0 關注
  • 220 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號