json: cannot unmarshal number into Go value of type string

result := `{"name":"my name is key1", "value":7200}`

res := make(map[string]string)
err := json.Unmarshal([]byte(result), &res)
if err != nil {
    // 此处会报错 json: cannot unmarshal number into Go value of type string
    panic(err)
}

// 原因  result value这个key是整型,无法使用 map[string]string 来接收

// 修改如下
type ResData struct {
	Name string `json:"name"`
	Value  int     `json:"value"`
}
var data ResData
err := json.Unmarshal([]byte(res), &data)
if err != nil {
     panic(err.Error())
}

  

posted @ 2022-06-15 17:34  php的自我修养  阅读(3704)  评论(0编辑  收藏  举报