Golang的JSON解码在数组上失败。

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

Golang json decoding fails on Array

问题

我有一个对象,如下所示:

  1. a = [{
  2. "name": "rdj",
  3. "place": "meh",
  4. "meh": ["bow", "blah"]
  5. }]

我定义了一个结构体,如下所示:

  1. type first struct {
  2. A []one
  3. }
  4. type one struct {
  5. Place string `json:"place"`
  6. Name string `json:"name"`
  7. }

当我在代码中使用相同的结构体时:

  1. func main() {
  2. res, _ := http.Get("http://127.0.0.1:8080/sample/")
  3. defer res.Body.Close()
  4. var some first
  5. rd := json.NewDecoder(res.Body)
  6. err := rd.Decode(&some)
  7. errorme(err)
  8. fmt.Printf("%+v\n", some)
  9. }

我得到以下错误:

  1. panic: json: cannot unmarshal array into Go value of type main.first

我的理解是:

type first 定义了一个数组,数组中包含了 type one 定义的数据结构。

英文:

I have an object like:

  1. a = [{
  2. "name": "rdj",
  3. "place": "meh",
  4. "meh" : ["bow", "blah"]
  5. }]

I defined a struct like:

  1. type first struct {
  2. A []one
  3. }
  4. type one struct {
  5. Place string `json: "place"`
  6. Name string `json: "name"`
  7. }

when I use the same in code like:

  1. func main() {
  2. res, _ := http.Get("http://127.0.0.1:8080/sample/")
  3. defer res.Body.Close()
  4. var some first
  5. rd := json.NewDecoder(res.Body)
  6. err := rd.Decode(&some)
  7. errorme(err)
  8. fmt.Printf("%+v\n", some)
  9. }

I get the below error:

  1. panic: json: cannot unmarshal array into Go value of type main.first

My understanding is:

type first defines the array and within that array is data structure defined in type one.

答案1

得分: 2

JSON是一个对象数组。使用以下类型:

  1. type one struct { // 使用结构体表示JSON对象
  2. Place string `json:"place"`
  3. Name string `json:"name"`
  4. }
  5. ...
  6. var some []one // 使用切片表示JSON数组
  7. rd := json.NewDecoder(res.Body)
  8. err := rd.Decode(&some)

问题中的类型与此JSON结构匹配:

  1. {
  2. "A": [
  3. {
  4. "name": "rdj",
  5. "place": "meh",
  6. "meh": ["bow", "blah"]
  7. }
  8. ]
  9. }
英文:

The JSON is an array of objects. Use these types:

  1. type one struct { // Use struct for JSON object
  2. Place string `json: "place"`
  3. Name string `json: "name"`
  4. }
  5. ...
  6. var some []one // Use slice for JSON array
  7. rd := json.NewDecoder(res.Body)
  8. err := rd.Decode(&some)

The the types in the question match this JSON structure:

  1. {"A": [{
  2. "name": "rdj",
  3. "place": "meh",
  4. "meh" : ["bow", "blah"]
  5. }]}

答案2

得分: 1

@rickydj,

  1. 如果你需要将“first”作为一个独立的类型:
  1. type first []one
  1. 如果你不介意将“first”作为一个独立的类型,可以按照上面 @Mellow Marmot 的建议简化为:
  1. var some []one
英文:

@rickydj,

  1. If you need to have "first" as a separate type:
  1. type first []one
  1. If you do not care about having "first" as a separate type, just cut down to
  1. var some []one

as @Mellow Marmot suggested above.

huangapple
  • 本文由 发表于 2016年11月30日 02:51:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/40873562.html
匿名

发表评论

匿名网友

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

确定