使用Golang的json.NewDecoder / json.NewEncoder。

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

Using Golang json.NewDecoder / json.NewEncoder

问题

我是你的中文翻译助手,以下是翻译好的内容:

我是一个完全不懂Go语言的新手,我正在尝试理解我在这里缺少了什么。我期望使用dec.Decode循环遍历JSON值,并最终得到一个响应的映射。但我得到的是整个JSON字符串作为映射的第一个元素的键。我错过了什么?

示例响应:

2015/03/02 10:03:16 map[error:invalid_request error_description:that is not a recognized WePay API call error_code:1001] map[string]interface {}

  1. package main
  2. import (
  3. "encoding/json"
  4. "io"
  5. "log"
  6. "net/http"
  7. "reflect"
  8. )
  9. func main() {
  10. var v map[string]interface{}
  11. resp, err := http.Get("https://wepayapi.com/v2/")
  12. if err != nil {
  13. log.Println("Error: " + err.Error())
  14. }
  15. defer resp.Body.Close()
  16. // resp.Body is an io.ReadCloser... NewDecoder expects an io.Reader
  17. dec := json.NewDecoder(resp.Body)
  18. // Decode reads the next JSON-encoded value from its input and stores it in the value pointed to by v.
  19. for err := dec.Decode(&v); err != nil && err != io.EOF; {
  20. log.Println("ERROR: " + err.Error())
  21. return
  22. }
  23. log.Println(v, reflect.TypeOf(v))
  24. }
英文:

I'm a total noob at go and I'm trying to understand what I'm missing here. I'm expecting to use dec.Decode to loop over the json values and end up with a map of the response. What I'm getting is the entire json string as the key to the first element of the map. What am I missing?

Example response:

2015/03/02 10:03:16 map[error:invalid_request error_description:that is not a recognized WePay API call error_code:1001] map[string]interface {}

  1. package main
  2. import (
  3. "encoding/json"
  4. "io"
  5. "log"
  6. "net/http"
  7. "reflect"
  8. )
  9. func main() {
  10. var v map[string]interface{}
  11. resp, err := http.Get("https://wepayapi.com/v2/")
  12. if err != nil {
  13. log.Println("Error: " + err.Error())
  14. }
  15. defer resp.Body.Close()
  16. // resp.Body is an io.ReadCloser... NewDecoder expects an io.Reader
  17. dec := json.NewDecoder(resp.Body)
  18. // Decode reads the next JSON-encoded value from its input and stores it in the value pointed to by v.
  19. for err := dec.Decode(&v); err != nil && err != io.EOF; {
  20. log.Println("ERROR: " + err.Error())
  21. return
  22. }
  23. log.Println(v, reflect.TypeOf(v))
  24. }

答案1

得分: 2

Decoder会一次性解码整个JSON值(在这种情况下是错误对象),你不需要在循环中调用它:

  1. if err := dec.Decode(&v); err != nil {
  2. log.Println("ERROR: " + err.Error())
  3. return
  4. }

作为响应,你会得到一个等价于这个JSON的映射:

  1. {
  2. "error": "invalid_request",
  3. "error_description": "that is not a recognized WePay API call",
  4. "error_code": 1001
  5. }

结果:

  1. map[string]interface{} {
  2. "error": "invalid_request",
  3. "error_description": "that is not a recognized WePay API call",
  4. "error_code": 1001,
  5. }
英文:

Decoder will decode the whole JSON value at once (in this case the error object), you don;y have to call it in a loop:

  1. if err := dec.Decode(&v); err != nil {
  2. log.Println("ERROR: " + err.Error())
  3. return
  4. }

As a response you get a map equavalent of this JSON:

  1. {"error":"invalid_request","error_description":"that is not a recognized WePay API call","error_code":1001}

Result:

  1. map[string]interface{} {
  2. "error":"invalid_request",
  3. "error_description":"that is not a recognized WePay API call",
  4. "error_code":1001,
  5. }

huangapple
  • 本文由 发表于 2015年3月3日 00:11:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/28814366.html
匿名

发表评论

匿名网友

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

确定