在 go lang 中循环/迭代第二级嵌套 JSON

考虑以下代码:


package main


import (

"encoding/json"

"fmt"

"reflect"

)



func main() {  

    //Creating the maps for JSON

    m := map[string]interface{}{}


    //Parsing/Unmarshalling JSON encoding/json

    err := json.Unmarshal([]byte(input), &m)


    fmt.Println("\nReflect type of Parsing/Unmarshalling Error Object:\n",reflect.TypeOf(err))

    fmt.Println("\nParsing/Unmarshalling Error Object:\n",err)

    if err != nil {

        panic(err)

    }


    fmt.Println("\nParsed JSON is as follows:\n",m)

    fmt.Println("\nReflect type of parsed json object:\n", reflect.TypeOf(m))


    for firstLvlkey, firstLvlValue := range m { 

        fmt.Println("First Level Key:", firstLvlkey)

        fmt.Println("First Level Key reflect type of :", reflect.TypeOf(firstLvlkey))


        fmt.Println("First Level Value:", firstLvlValue)

        fmt.Println("First Level Value reflect type of :", reflect.TypeOf(firstLvlValue))

         // <===============================>

         //Here I want to iterate/loop over innerJSON1, InnerJSON2 then reach to level InnerInnerJSONArray - fld1 and fld2

         // <===============================>


    }

}


const input = `

{

    "outterJSON":{

        "innerJSON1":{

            "value1":10,

            "value2":22

            ,

            "InnerInnerArray": [ "test1" , "test2"],

            "InnerInnerJSONArray": [ {"fld1" : "val1"} , {"fld2" : "val2"} ]

            },

            "InnerJSON2":"NoneValue"

        }

    }

    `

我有一些要求,比如我想读取/获取类型中的所有键和值以String进行某些处理,我无法定义,struct因为我将获得动态 JSON 输入(例如InnerInnerArray作为字符串,然后第二级循环会给我索引数组并处理每个具有键fld1和 的JSON val1。


我希望遍历其中包含的每个键/值对,浏览地图的最有效方法是什么?


注意:我是 Go-lang 的新手,也非常欢迎您对问题提出建议/改进。


犯罪嫌疑人X
浏览 186回答 3
3回答

慕姐4208626

请参阅此博客条目,该条目彻底涵盖了该主题,特别是解码任意数据部分。使用它你可以做这样的事情:(游乐场示例)package mainimport (&nbsp; &nbsp; "encoding/json"&nbsp; &nbsp; "fmt"&nbsp; &nbsp;&nbsp;)func main() {&nbsp; &nbsp; // Creating the maps for JSON&nbsp; &nbsp; m := map[string]interface{}{}&nbsp; &nbsp; // Parsing/Unmarshalling JSON encoding/json&nbsp; &nbsp; err := json.Unmarshal([]byte(input), &m)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; parseMap(m)}func parseMap(aMap map[string]interface{}) {&nbsp; &nbsp; for key, val := range aMap {&nbsp; &nbsp; &nbsp; &nbsp; switch concreteVal := val.(type) {&nbsp; &nbsp; &nbsp; &nbsp; case map[string]interface{}:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(key)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parseMap(val.(map[string]interface{}))&nbsp; &nbsp; &nbsp; &nbsp; case []interface{}:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(key)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parseArray(val.([]interface{}))&nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(key, ":", concreteVal)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}func parseArray(anArray []interface{}) {&nbsp; &nbsp; for i, val := range anArray {&nbsp; &nbsp; &nbsp; &nbsp; switch concreteVal := val.(type) {&nbsp; &nbsp; &nbsp; &nbsp; case map[string]interface{}:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Index:", i)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parseMap(val.(map[string]interface{}))&nbsp; &nbsp; &nbsp; &nbsp; case []interface{}:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Index:", i)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parseArray(val.([]interface{}))&nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Index", i, ":", concreteVal)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}const input = `{&nbsp; &nbsp; "outterJSON": {&nbsp; &nbsp; &nbsp; &nbsp; "innerJSON1": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "value1": 10,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "value2": 22,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "InnerInnerArray": [ "test1" , "test2"],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "InnerInnerJSONArray": [{"fld1" : "val1"} , {"fld2" : "val2"}]&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; "InnerJSON2":"NoneValue"&nbsp; &nbsp; }}`这将打印:&nbsp; &nbsp; //outterJSON&nbsp; &nbsp; //innerJSON1&nbsp; &nbsp; //InnerInnerJSONArray&nbsp; &nbsp; //Index: 0&nbsp; &nbsp; //fld1 : val1&nbsp; &nbsp; //Index: 1&nbsp; &nbsp; //fld2 : val2&nbsp; &nbsp; //value1 : 10&nbsp; &nbsp; //value2 : 22&nbsp; &nbsp; //InnerInnerArray&nbsp; &nbsp; //Index 0 : test1&nbsp; &nbsp; //Index 1 : test2&nbsp; &nbsp; //InnerJSON2 : NoneValue关键是在处理接口类型时必须使用类型断言。类型开关可以根据需要轻松确定类型。该代码将递归地遍历任何嵌套数组或映射,因此您可以根据需要添加任意数量的级别并获取所有值。

慕田峪7331174

这里和这里有相关的问题(可能还有其他问题)。有一些更复杂的 JSON 解析 API 可以让您的工作更轻松。一个例子是stretchr/objx。一个使用 objx 的例子:document, err := objx.FromJSON(json)// TODO handle errdocument.Get("path.to.field[0].you.want").Str()当您真的不知道 JSON 结构是什么时,这会起作用。但是,如果您提前知道 JSON 输入的结构,则首选方法是使用结构来描述它并使用标准 API 进行编组。

qq_花开花谢_0

您需要解析 JSON,然后通过结构递归检查所包含值的类型并以某种方式处理它们。下面的示例函数接受一个*interface{}(指向任何类型的指针)和一个字符串、整数和对象指针的处理函数,它产生它发现的项目:func eachJsonValue(obj *interface{}, handler func(*string, *int, *interface{})) {&nbsp; if obj == nil {&nbsp; &nbsp; return&nbsp; }&nbsp; // Yield all key/value pairs for objects.&nbsp; o, isObject := (*obj).(map[string]interface{})&nbsp; if isObject {&nbsp; &nbsp; for k, v := range o {&nbsp; &nbsp; &nbsp; handler(&k, nil, &v)&nbsp; &nbsp; &nbsp; eachJsonValue(&v, handler)&nbsp; &nbsp; }&nbsp; }&nbsp; // Yield each index/value for arrays.&nbsp; a, isArray := (*obj).([]interface{})&nbsp; if isArray {&nbsp; &nbsp; for i, x := range a {&nbsp; &nbsp; &nbsp; handler(nil, &i, &x)&nbsp; &nbsp; &nbsp; eachJsonValue(&x, handler)&nbsp; &nbsp; }&nbsp; }&nbsp; // Do nothing for primitives since the handler got them.}如下所示调用它会打印列出的结果。当然,您的处理程序函数可以对已知的键/值(例如“fld1”)执行一些特殊操作:func main() {&nbsp; // Parse the JSON.&nbsp; var obj interface{}&nbsp; json.Unmarshal([]byte(input), &obj) // XXX: check the error value.&nbsp; // Handle object key/value pairs and array index/items.&nbsp; eachJsonValue(&obj, func(key *string, index *int, value *interface{}) {&nbsp; &nbsp; if key != nil { // It's an object key/value pair...&nbsp; &nbsp; &nbsp; fmt.Printf("OBJ: key=%q, value=%#v\n", *key, *value)&nbsp; &nbsp; } else { // It's an array item...&nbsp; &nbsp; &nbsp; fmt.Printf("ARR: index=%d, value=%#v\n", *index, *value)&nbsp; &nbsp; }&nbsp; })}// OBJ: key="outterJSON", value=map[string]interface {}{...}// OBJ: key="innerJSON1", value=map[string]interface {}{...}// OBJ: key="value1", value=10// OBJ: key="value2", value=22// OBJ: key="InnerInnerArray", value=[]interface {}{...}// ARR: index=0, value="test1"// ARR: index=1, value="test2"// OBJ: key="InnerInnerJSONArray", value=[]interface {}{...}// ARR: index=0, value=map[string]interface {}{...}// OBJ: key="fld1", value="val1"// ARR: index=1, value=map[string]interface {}{...}// OBJ: key="fld2", value="val2"// OBJ: key="InnerJSON2", value="NoneValue"
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go