dongyan6503
2015-03-31 10:10
浏览 63
已采纳

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

Consider the following code:

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("
Reflect type of Parsing/Unmarshalling Error Object:
",reflect.TypeOf(err))
    fmt.Println("
Parsing/Unmarshalling Error Object:
",err)
    if err != nil {
        panic(err)
    }

    fmt.Println("
Parsed JSON is as follows:
",m)
    fmt.Println("
Reflect type of parsed json object:
", 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"
        }
    }
    `

I have some requirement like I want to read/get all the Key and value in String type for some processing adn I can't define the struct because I will be getting dynamic JSON input (e.g InnerInnerArray as a string then second level loop will give me index of array and process each JSON having key fld1 and val1).

I wish to iterate over every key/value pair contained within it, what is the most efficient way of going through the map?

Note: I am Newbie for Go-lang, your suggestion/improvement on question is also most welcome.

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • duanjiao5082 2015-04-01 03:06
    已采纳

    See this blog entry which thoroughly covers this subject, specifically the section Decoding arbitrary data. Using that you can do something like this: (playground example)

    package main
    
    import (
        "encoding/json"
        "fmt"    
    )
    
    func main() {
        // Creating the maps for JSON
        m := map[string]interface{}{}
    
        // Parsing/Unmarshalling JSON encoding/json
        err := json.Unmarshal([]byte(input), &m)
    
        if err != nil {
            panic(err)
        }
        parseMap(m)
    }
    
    func parseMap(aMap map[string]interface{}) {
        for key, val := range aMap {
            switch concreteVal := val.(type) {
            case map[string]interface{}:
                fmt.Println(key)
                parseMap(val.(map[string]interface{}))
            case []interface{}:
                fmt.Println(key)
                parseArray(val.([]interface{}))
            default:
                fmt.Println(key, ":", concreteVal)
            }
        }
    }
    
    func parseArray(anArray []interface{}) {
        for i, val := range anArray {
            switch concreteVal := val.(type) {
            case map[string]interface{}:
                fmt.Println("Index:", i)
                parseMap(val.(map[string]interface{}))
            case []interface{}:
                fmt.Println("Index:", i)
                parseArray(val.([]interface{}))
            default:
                fmt.Println("Index", i, ":", concreteVal)
    
            }
        }
    }
    
    const input = `
    {
        "outterJSON": {
            "innerJSON1": {
                "value1": 10,
                "value2": 22,
                "InnerInnerArray": [ "test1" , "test2"],
                "InnerInnerJSONArray": [{"fld1" : "val1"} , {"fld2" : "val2"}]
            },
            "InnerJSON2":"NoneValue"
        }
    }
    `
    

    This will print:

        //outterJSON
        //innerJSON1
        //InnerInnerJSONArray
        //Index: 0
        //fld1 : val1
        //Index: 1
        //fld2 : val2
        //value1 : 10
        //value2 : 22
        //InnerInnerArray
        //Index 0 : test1
        //Index 1 : test2
        //InnerJSON2 : NoneValue
    

    The key thing is that you have to use type assertion when working with interface types. The type switch makes it easy to determine the type as needed. The code will recursively range through any nested array or map so you can add as many levels as you wish and get all your values.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dpo15099 2015-03-31 15:00

    You'll need to parse the JSON and then recurse through the structure inspecting the types of the contained values and handling them in some way.

    The example function below takes an *interface{} (pointer to any type) and a handler function of string, int, and object pointers to which it yields the items it discovers:

    func eachJsonValue(obj *interface{}, handler func(*string, *int, *interface{})) {
      if obj == nil {
        return
      }
      // Yield all key/value pairs for objects.
      o, isObject := (*obj).(map[string]interface{})
      if isObject {
        for k, v := range o {
          handler(&k, nil, &v)
          eachJsonValue(&v, handler)
        }
      }
      // Yield each index/value for arrays.
      a, isArray := (*obj).([]interface{})
      if isArray {
        for i, x := range a {
          handler(nil, &i, &x)
          eachJsonValue(&x, handler)
        }
      }
      // Do nothing for primitives since the handler got them.
    }
    

    Calling it as demonstrated below will print the listed results. Your handler function could, of course, do something special with known key/values such as "fld1":

    func main() {
      // Parse the JSON.
      var obj interface{}
      json.Unmarshal([]byte(input), &obj) // XXX: check the error value.
    
      // Handle object key/value pairs and array index/items.
      eachJsonValue(&obj, func(key *string, index *int, value *interface{}) {
        if key != nil { // It's an object key/value pair...
          fmt.Printf("OBJ: key=%q, value=%#v
    ", *key, *value)
        } else { // It's an array item...
          fmt.Printf("ARR: index=%d, value=%#v
    ", *index, *value)
        }
      })
    }
    
    // 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"
    
    评论
    解决 无用
    打赏 举报
  • dongxia8656 2015-04-02 09:55

    There are related questions here and here (and possibly others).

    There are some more sophisticated JSON parsing APIs that make your job easier. An example is stretchr/objx.

    An example of using objx:

    document, err := objx.FromJSON(json)
    // TODO handle err
    document.Get("path.to.field[0].you.want").Str()
    

    This works when you really don't know what the JSON structure will be. However, if you do know the structure of your JSON input ahead of time, the preferred way is to describe it with structs and use the standard API for marshalling.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题