如何在Go语言中获取这种类型的数据?

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

How can I get this type of data in Go lang?

问题

你可以使用Go语言中的结构体来解析这个API响应数据。首先,你需要定义一个与API响应数据结构相匹配的结构体类型。然后,使用json.Unmarshal函数将API响应数据解析为该结构体类型的实例。

以下是一个示例代码,展示了如何在Go语言中解析该API响应数据:

package main

import (
	"encoding/json"
	"fmt"
)

type Response struct {
	Result  int         `json:"result"`
	Message string      `json:"message"`
	Pds     []Product   `json:"pds"`
	Request RequestInfo `json:"request"`
}

type Product struct {
	State string `json:"state"`
	Code  int    `json:"code"`
	Name  string `json:"name"`
	Price int    `json:"price"`
}

type RequestInfo struct {
	Op string `json:"op"`
}

func main() {
	data := `{
		"result": 1,
		"message": "",
		"pds": [
			{
				"state": "Y",
				"code": 13,
				"name": "AAA",
				"price": 39900
			},
			{
				"state": "Y",
				"code": 12,
				"name": "BBB",
				"price": 38000
			}
		],
		"request": {
			"op": "new"
		}
	}`

	var response Response
	err := json.Unmarshal([]byte(data), &response)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}

	fmt.Println("Result:", response.Result)
	fmt.Println("Message:", response.Message)
	fmt.Println("Request Op:", response.Request.Op)

	for _, pd := range response.Pds {
		fmt.Println("Product Name:", pd.Name)
		fmt.Println("Product Price:", pd.Price)
	}
}

在上面的示例代码中,我们定义了三个结构体类型:ResponseProductRequestInfo,分别对应API响应数据的不同部分。然后,我们使用json.Unmarshal函数将API响应数据解析为Response类型的实例。通过访问该实例的字段,你可以获取到相应的数据。

希望这可以帮助到你!如果你有任何其他问题,请随时问我。

英文:

This is API response data, looks like this.

{   
"result":1,
"message":"",
"pds": [
{
"state":"Y",
"code":13,
"name":"AAA",
"price":39900,
},
{
"state":"Y",
"code":12,
"name":"BBB",
"price":38000,
}
],
"request":
{
"op":"new",
}
}

How can I get this data in Go lang?
I tried json.Unmarshall and get with map[string]interface{} but it looks like I used the wrong type to get the data.
Should I use structure??

答案1

得分: 2

你需要创建一个结构体来正确处理,如果你不想让json.Unmarshal的输出是map[string]interface{}类型的。

如果你将这个JSON对象映射到一个Go结构体,你会发现以下结构:

type APIResponse struct {
	Result  int    `json:"result"`
	Message string `json:"message"`
	Pds     []struct {
		State string     `json:"state"`
		Code  int        `json:"code"`
		Name  string     `json:"name"`
		Price float64    `json:"price"`
	} `json:"pds"`
	Request struct {
		Op string `json:"op"`
	} `json:"request"`
}

你还可以在这里找到一个很棒的工具,用于将JSON对象转换为Go结构体。

英文:

You need to create a struct to handle this properly if you don't want the json.Unmarshall output to be a map[string]interface{}.

If you map this JSON object to a Go struct you find the following structure:

type APIResponse struct {
	Result  int    `json:"result"`
	Message string `json:"message"`
	Pds     []struct {
		State string     `json:"state"`
		Code  int        `json:"code"`
		Name  string     `json:"name"`
		Price float64    `json:"price"`
	} `json:"pds"`
	Request struct {
		Op string `json:"op"`
	} `json:"request"`
}

You also can find a great tool to convert JSON objects to Go structs here

答案2

得分: -1

如果你不想使用原生的json.Unmarshall,这里有一个很好的选择,可以使用包go-simplejson

文档

英文:

If you don't want to use the native json.Unmarshall, here's a good option to use the package go-simplejson.

Documentation

huangapple
  • 本文由 发表于 2022年3月20日 12:03:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/71543934.html
匿名

发表评论

匿名网友

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

确定