Unmarshaling nested JSON objects

后端 未结 8 1812
轻奢々
轻奢々 2020-11-27 11:37

There are a few questions on the topic but none of them seem to cover my case, thus I\'m creating a new one.

I have JSON like the following:

{\"foo\"         


        
8条回答
  •  没有蜡笔的小新
    2020-11-27 11:47

    Assign the values of nested json to struct until you know the underlying type of json keys:-

    package main
    
    import (
        "encoding/json"
        "fmt"
    )
    
    // Object
    type Object struct {
        Foo map[string]map[string]string `json:"foo"`
        More string `json:"more"`
    }
    
    func main(){
        someJSONString := []byte(`{"foo":{ "bar": "1", "baz": "2" }, "more": "text"}`)
        var obj Object
        err := json.Unmarshal(someJSONString, &obj)
        if err != nil{
            fmt.Println(err)
        }
        fmt.Println("jsonObj", obj)
    }
    

提交回复
热议问题