如何从这种类型的 JSON 响应中提取数据?

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

how to grab data from this type of json response?

问题

当解析一个GET请求后,我该如何获取'personaname'和'realname'的值?我用以下结构体和代码进行解析,但是没有得到任何结果。我对JSON和这种类型的JSON不太熟悉,有什么提示吗?(下面是响应的JSON)

type Response struct {
    personaname string
    realname    string
}

sb := string(res.Body())
var response []Response
json.Unmarshal([]byte(sb), &response)
{
  "response": {
    "players": [
      {
        "personaname": "alfred",
        "realname": "alfred d"
      },
      {
        "personaname": "EJ",
        "realname": "Erik Johnson"
      }
    ]
  }
}
英文:

when parsing after a get request, how would I grab the 'personaname' and 'realname' values? the way I'm doing it with this struct

type Response struct {
    personaname string
    realname string
}

and this code

sb := string(res.Body())
var response []Response
json.Unmarshal([]byte(sb), &response)

isn't giving me anything. any tips? I'm not really familiar with json and how this kind of json works. (below is the response json)

{
  "response": {
    "players": [
      {
        "personaname": "alfred",
	    "realname": "alfred d"
      },
      {
        "personaname": "EJ",
        "realname": "Erik Johnson"
      }
    ]
  }
}

答案1

得分: 3

你需要定义一个与JSON完全匹配的结构,可以是部分或完全匹配,但必须从根开始。

package main

import (
	"encoding/json"
	"fmt"
)

type Body struct {
	Response struct {
		Players []Player
	}
}

type Player struct {
	PersonaName string
	Realname    string
}

func main() {

	body := []byte(`{
          "response": {
            "players": [
              {
                "personaname": "alfred",
                "realname": "alfred d"
              },
              {
                "personaname": "EJ",
                "realname": "Erik Johnson"
              }
            ]
          }
        }`)

	var parsed Body
	if err := json.Unmarshal(body, &parsed); err != nil {
		panic(err)
	}

	fmt.Printf("%+v", parsed.Response.Players)

}
英文:

You need to define a full structure that matches the JSON, partially or totally but from the root.

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

package main

import (
	"encoding/json"
	"fmt"
)

type Body struct {
	Response struct {
		Players []Player
	}
}

type Player struct {
	PersonaName string
	Realname    string
}

func main() {

	body := []byte(`{
          "response": {
            "players": [
              {
                "personaname": "alfred",
                "realname": "alfred d"
              },
              {
                "personaname": "EJ",
                "realname": "Erik Johnson"
              }
            ]
          }
        }`)

	var parsed Body
	if err := json.Unmarshal(boddy, &parsed); err != nil {
		panic(err)
	}

	fmt.Printf("%+v", parsed.Response.Players)

}

答案2

得分: -1

实际上,你的结构不会起作用,因为你的请求体是一个对象数组。
使用这种结构,你只能等待像这样的请求体{ "personaname": "alfred", "realname": "alfred d"},只有一个对象。

我建议你创建一个新的结构,它接受一个指向你的结构响应的切片。
你可以像这样定义:

type Response struct {
    Personaname string
    Realname    string
}

type Rsp struct {
    Players []Response `json:"players"`
}

这样你就可以通过Rsp结构来处理包含多个对象的响应体了。

英文:

Actually your structure won't work since you body request is an array of objects.
With this structure you wait for body like this{ "personaname": "alfred", "realname": "alfred d"} with just one object.

I recommend you to create a new structure that takes a slice pointing to your structure response.
You would have like

   type Response struct {
    Personaname string
    Realname string
   }
    
    type Rsp struct {

    	Players []Response `json:"players"`
    }

huangapple
  • 本文由 发表于 2021年7月18日 05:22:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/68424372.html
匿名

发表评论

匿名网友

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

确定