如何使用Golang的json.Unmarshal函数解析这个复杂的Json数据?

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

How to unmarshal this complex Json data with Golang func json.Unmarshal?

问题

我有这样的JSON对象:

  1. {
  2. "action":"GetLoad",
  3. "resource_id":"lb-cdvyel0v",
  4. "ret_code":0,
  5. "meter_set":[
  6. {
  7. "data_set":[
  8. {
  9. "data":[
  10. [
  11. 1478672400,
  12. [
  13. 1,
  14. 0
  15. ]
  16. ],
  17. [
  18. 1,
  19. 0
  20. ],
  21. [
  22. 0,
  23. 0
  24. ],
  25. [
  26. 8,
  27. 0
  28. ],
  29. [
  30. 1,
  31. 0
  32. ]
  33. ],
  34. "eip_id":"eip-jf79ljt7"
  35. },
  36. {
  37. "data":[
  38. [
  39. 1478693280,
  40. [
  41. 0,
  42. 0
  43. ]
  44. ],
  45. [
  46. 1,
  47. 0
  48. ],
  49. [
  50. 0,
  51. 0
  52. ]
  53. ],
  54. "eip_id":"eip-mw6n6wg0"
  55. }
  56. ],
  57. "meter_id":"uaffic"
  58. }
  59. ]
  60. }

我尝试通过以下方式解决问题:

  1. type CommonResponse struct {
  2. Action string `json:"action"`
  3. JobID string `json:"job_id"`
  4. RetCode int `json:"ret_code"`
  5. Message string `json:"message"`
  6. }
  7. type GetLoadResponse struct {
  8. MeterSet []LoadMeter `json:"meter_set"`
  9. ResourceId string `json:"resource_id"`
  10. CommonResponse
  11. }
  12. type LoadMeter struct {
  13. MeterID string `json:"meter_id"`
  14. DataSet LoadDataSet `json:"data_set"`
  15. }
  16. type LoadDataSet struct {
  17. EipID string `json:"eip_id"`
  18. Data []interface{} `json:"data"`
  19. }
  20. func Get(response interface{}) {
  21. str := `{ "action": "GetLoad", "resource_id": "lb-cdvyel0v", "ret_code": 0, "meter_set": [ { "data_set": [ { "data": [ [ 1478672400, [ 1, 0 ] ], [ 1, 0 ], [ 0, 0 ], [ 8, 0 ], [ 1, 0 ] ], "eip_id": "eip-jf79ljt7" }, { "data": [ [ 1478693280, [ 0, 0 ] ], [ 1, 0 ], [ 0, 0 ] ], "eip_id": "eip-mw6n6wg0" } ], "meter_id": "uaffic" } ] }`
  22. result := []byte(str)
  23. err := json.Unmarshal(result, &response)
  24. fmt.Println(err)
  25. }
  26. func main() {
  27. var res GetLoadResponse
  28. Get(&res)
  29. //Get(res) // Will not be wrong, but res is null
  30. fmt.Println(res)
  31. }

然后,我得到了这个错误:无法将数组解组为类型为main.LoadDataSet的Go值。

JSON数据的图像:

如何使用Golang的json.Unmarshal函数解析这个复杂的Json数据?

英文:

I have the json object like this :

  1. {
  2. "action":"GetLoad",
  3. "resource_id":"lb-cdvyel0v",
  4. "ret_code":0,
  5. "meter_set":[
  6. {
  7. "data_set":[
  8. {
  9. "data":[
  10. [
  11. 1478672400,
  12. [
  13. 1,
  14. 0
  15. ]
  16. ],
  17. [
  18. 1,
  19. 0
  20. ],
  21. [
  22. 0,
  23. 0
  24. ],
  25. [
  26. 8,
  27. 0
  28. ],
  29. [
  30. 1,
  31. 0
  32. ]
  33. ],
  34. "eip_id":"eip-jf79ljt7"
  35. },
  36. {
  37. "data":[
  38. [
  39. 1478693280,
  40. [
  41. 0,
  42. 0
  43. ]
  44. ],
  45. [
  46. 1,
  47. 0
  48. ],
  49. [
  50. 0,
  51. 0
  52. ]
  53. ],
  54. "eip_id":"eip-mw6n6wg0"
  55. }
  56. ],
  57. "meter_id":"uaffic"
  58. }
  59. ]
  60. }

and I try to solve the problem like this:

  1. type CommonResponse struct {
  2. Action string `json:"action"`
  3. JobID string `json:"job_id"`
  4. RetCode int `json:"ret_code"`
  5. Message string `json:"message"`
  6. }
  7. type GetLoadResponse struct {
  8. MeterSet []LoadMeter `json:"meter_set"`
  9. ResourceId string `json:"resource_id"`
  10. CommonResponse
  11. }
  12. type LoadMeter struct {
  13. MeterID string `json:"meter_id"`
  14. DataSet LoadDataSet `json:"data_set"`
  15. }
  16. type LoadDataSet struct {
  17. EipID string `json:"eip_id"`
  18. Data []interface{} `json:"data"`
  19. }
  20. func Get(response interface{}) {
  21. str := `{ "action": "GetLoad", "resource_id": "lb-cdvyel0v", "ret_code": 0, "meter_set": [ { "data_set": [ { "data": [ [ 1478672400, [ 1, 0 ] ], [ 1, 0 ], [ 0, 0 ], [ 8, 0 ], [ 1, 0 ] ], "eip_id": "eip-jf79ljt7" }, { "data": [ [ 1478693280, [ 0, 0 ] ], [ 1, 0 ], [ 0, 0 ] ], "eip_id": "eip-mw6n6wg0" } ], "meter_id": "uaffic" } ] }`
  22. result := []byte(str)
  23. err := json.Unmarshal(result, &response)
  24. fmt.Println(err)
  25. }
  26. func main() {
  27. var res GetLoadResponse
  28. Get(&res)
  29. //Get(res) // Will not be wrong, but res is null
  30. fmt.Println(res)
  31. }

and then, I got this error:
cannot unmarshal array into Go value of type main.LoadDataSet

Playground: https://play.golang.org/p/ywFUu2MVNR

Image of JSON data:

如何使用Golang的json.Unmarshal函数解析这个复杂的Json数据?

答案1

得分: 1

你的meter_set元素中的data_set是一个LoadDataSet数组。将你的LoadMeter更改为:

  1. type LoadMeter struct {
  2. MeterID string `json:"meter_id"`
  3. DataSet []LoadDataSet `json:"data_set"`
  4. }
英文:

Your data_set in meter_set element is an array of LoadDataSet. Change your LoadMeter to:

  1. type LoadMeter struct {
  2. MeterID string `json:"meter_id"`
  3. DataSet []LoadDataSet `json:"data_set"`
  4. }

huangapple
  • 本文由 发表于 2016年11月10日 11:53:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/40519692.html
匿名

发表评论

匿名网友

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

确定