美文网首页
遍历json

遍历json

作者: jaydenZou1228 | 来源:发表于2019-08-15 23:54 被阅读0次
    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"
        }
    }
    `
    

    相关文章

      网友评论

          本文标题:遍历json

          本文链接:https://www.haomeiwen.com/subject/wedxsctx.html