how to Unmarshal json in golang

huangapple go评论116阅读模式
英文:

how to Unmarshal json in golang

问题

我有一个 JSON 数据:

  1. {"code":200,
  2. "msg":"success",
  3. "data":{"url":"https:\/\/mp.weixin.qq.com\/cgi-bin\/showqrcode?ticket=gQHQ7jwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyX3pqS0pMZlA4a1AxbEJkemhvMVoAAgQ5TGNYAwQsAQAA"}}

我定义了一个结构体:

  1. type Result struct {
  2. code int
  3. msg string `json:"msg"`
  4. data map[string]interface{} `json:"data"`
  5. }

对于这段代码:

  1. var res Result
  2. json.Unmarshal(body, &res)
  3. fmt.Println(res)

输出结果是:{0 map[]}

我想要获取 data 中的 url,该如何获取呢?

英文:

I have a json:

  1. {"code":200,
  2. "msg":"success",
  3. "data":{"url":"https:\/\/mp.weixin.qq.com\/cgi-bin\/showqrcode?ticket=gQHQ7jwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyX3pqS0pMZlA4a1AxbEJkemhvMVoAAgQ5TGNYAwQsAQAA"}}

and i define a struct :

  1. type Result struct {
  2. code int
  3. msg string `json:"msg"`
  4. data map[string]interface{} `json:"data"`
  5. }

for this code:

  1. var res Result
  2. json.Unmarshal(body, &res)
  3. fmt.Println(res)

the output is: {0 map[]}

i want to get url in data, how to get it?

答案1

得分: 4

你应该通过将字段(CodeMsgData)的首字母大写来导出Result的字段(codemsgdata),以便访问(设置/获取)它们:

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. type Result struct {
  7. Code int `json:"code"`
  8. Msg string `json:"msg"`
  9. Data map[string]interface{} `json:"data"`
  10. }
  11. func main() {
  12. str := `{"code":200,"msg":"success","data":{"url":"https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=gQHQ7jwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyX3pqS0pMZlA4a1AxbEJkemhvMVoAAgQ5TGNYAwQsAQAA"}}`
  13. var res Result
  14. err := json.Unmarshal([]byte(str), &res)
  15. fmt.Println(err)
  16. fmt.Println(res)
  17. }

在https://play.golang.org/p/23ah8e_hCa上运行代码。

相关问题:https://stackoverflow.com/questions/24837432/golang-capitals-in-struct-fields

英文:

You should export fields (code, msg, data) for Result by capitalizing the first letter of fields (Code, Msg, Data) to access (set/get) them:

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. type Result struct {
  7. Code int `json:"code"`
  8. Msg string `json:"msg"`
  9. Data map[string]interface{} `json:"data"`
  10. }
  11. func main() {
  12. str := `{"code":200,"msg":"success","data":{"url":"https:\/\/mp.weixin.qq.com\/cgi-bin\/showqrcode?ticket=gQHQ7jwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyX3pqS0pMZlA4a1AxbEJkemhvMVoAAgQ5TGNYAwQsAQAA"}}`
  13. var res Result
  14. err := json.Unmarshal([]byte(str), &res)
  15. fmt.Println(err)
  16. fmt.Println(res)
  17. }

Play the code on https://play.golang.org/p/23ah8e_hCa

Related question: https://stackoverflow.com/questions/24837432/golang-capitals-in-struct-fields

huangapple
  • 本文由 发表于 2016年12月28日 13:25:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/41355847.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定