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

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

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

问题

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

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

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. type Response struct {
  7. Result int `json:"result"`
  8. Message string `json:"message"`
  9. Pds []Product `json:"pds"`
  10. Request RequestInfo `json:"request"`
  11. }
  12. type Product struct {
  13. State string `json:"state"`
  14. Code int `json:"code"`
  15. Name string `json:"name"`
  16. Price int `json:"price"`
  17. }
  18. type RequestInfo struct {
  19. Op string `json:"op"`
  20. }
  21. func main() {
  22. data := `{
  23. "result": 1,
  24. "message": "",
  25. "pds": [
  26. {
  27. "state": "Y",
  28. "code": 13,
  29. "name": "AAA",
  30. "price": 39900
  31. },
  32. {
  33. "state": "Y",
  34. "code": 12,
  35. "name": "BBB",
  36. "price": 38000
  37. }
  38. ],
  39. "request": {
  40. "op": "new"
  41. }
  42. }`
  43. var response Response
  44. err := json.Unmarshal([]byte(data), &response)
  45. if err != nil {
  46. fmt.Println("Error:", err)
  47. return
  48. }
  49. fmt.Println("Result:", response.Result)
  50. fmt.Println("Message:", response.Message)
  51. fmt.Println("Request Op:", response.Request.Op)
  52. for _, pd := range response.Pds {
  53. fmt.Println("Product Name:", pd.Name)
  54. fmt.Println("Product Price:", pd.Price)
  55. }
  56. }

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

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

英文:

This is API response data, looks like this.

  1. {
  2. "result":1,
  3. "message":"",
  4. "pds": [
  5. {
  6. "state":"Y",
  7. "code":13,
  8. "name":"AAA",
  9. "price":39900,
  10. },
  11. {
  12. "state":"Y",
  13. "code":12,
  14. "name":"BBB",
  15. "price":38000,
  16. }
  17. ],
  18. "request":
  19. {
  20. "op":"new",
  21. }
  22. }

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结构体,你会发现以下结构:

  1. type APIResponse struct {
  2. Result int `json:"result"`
  3. Message string `json:"message"`
  4. Pds []struct {
  5. State string `json:"state"`
  6. Code int `json:"code"`
  7. Name string `json:"name"`
  8. Price float64 `json:"price"`
  9. } `json:"pds"`
  10. Request struct {
  11. Op string `json:"op"`
  12. } `json:"request"`
  13. }

你还可以在这里找到一个很棒的工具,用于将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:

  1. type APIResponse struct {
  2. Result int `json:"result"`
  3. Message string `json:"message"`
  4. Pds []struct {
  5. State string `json:"state"`
  6. Code int `json:"code"`
  7. Name string `json:"name"`
  8. Price float64 `json:"price"`
  9. } `json:"pds"`
  10. Request struct {
  11. Op string `json:"op"`
  12. } `json:"request"`
  13. }

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:

确定