Decode JSON with unknown structure

前端 未结 3 1768
时光取名叫无心
时光取名叫无心 2020-12-05 01:41

I want to get a string that represents a json like this one:

{ "votes": { "option_A": "3" } }

and inclu

3条回答
  •  情歌与酒
    2020-12-05 02:31

    package main
    
    import "encoding/json"
    
    func main() {
        in := []byte(`{ "votes": { "option_A": "3" } }`)
        var raw map[string]interface{}
        if err := json.Unmarshal(in, &raw); err != nil {
            panic(err)
        }
        raw["count"] = 1
        out, err := json.Marshal(raw)
        if err != nil {
            panic(err)
        }
        println(string(out))
    }
    

    https://play.golang.org/p/o8ZwvgsQmoO

提交回复
热议问题