Unmarshal JSON into map

前端 未结 1 529
伪装坚强ぢ
伪装坚强ぢ 2021-01-14 20:31

I have a really simple JSON file, something like this, but with thousands of strings:

{\"fruits\":[\"apple\",\"banana\",\"cherry\",\"date\"]}
相关标签:
1条回答
  • 2021-01-14 21:26

    here is example how you can Unmarshal to string list without any struct.

    package main
    
    import "fmt"
    import "encoding/json"
    
    func main() {
        src_json := []byte(`{"fruits":["apple","banana","cherry","date"]}`)
        var m map[string][]string
        err := json.Unmarshal(src_json, &m)
        if err != nil {
            panic(err)
        }
        fmt.Printf("%v", m["fruits"][0]) //apple
     }
    

    Or instead of String list you can use map[string][]interface{}

    0 讨论(0)
提交回复
热议问题