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

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

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

问题

我有这样的JSON对象:

{  
  "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"
      }
  ]
}

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

type CommonResponse struct {                                                                             
    Action  string `json:"action"`
    JobID   string `json:"job_id"`
    RetCode int    `json:"ret_code"`
    Message string `json:"message"`
}

type GetLoadResponse struct {
    MeterSet   []LoadMeter `json:"meter_set"`
    ResourceId string              `json:"resource_id"`
    CommonResponse
}
type LoadMeter struct {                                                                          
    MeterID string              `json:"meter_id"`
    DataSet LoadDataSet `json:"data_set"`
}     
  
type LoadDataSet struct {
    EipID string        `json:"eip_id"`
    Data  []interface{} `json:"data"`
}     

func Get(response interface{}) {
    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" } ] }`

    result := []byte(str)
    err := json.Unmarshal(result, &response)
    fmt.Println(err)
}
func main() {
    var res GetLoadResponse
    Get(&res)
    //Get(res) // Will not be wrong, but res is null
    fmt.Println(res)
}

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

JSON数据的图像:

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

英文:

I have the json object like this :

{  
  "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"
      }
  ]
}

and I try to solve the problem like this:

type CommonResponse struct {                                                                             
    Action  string `json:"action"`
    JobID   string `json:"job_id"`
    RetCode int    `json:"ret_code"`
    Message string `json:"message"`
}

type GetLoadResponse struct {
    MeterSet   []LoadMeter `json:"meter_set"`
    ResourceId string              `json:"resource_id"`
    CommonResponse
}
type LoadMeter struct {                                                                          
    MeterID string              `json:"meter_id"`
    DataSet LoadDataSet `json:"data_set"`
}     
  
type LoadDataSet struct {
    EipID string        `json:"eip_id"`
    Data  []interface{} `json:"data"`
}     

func Get(response interface{}) {
    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" } ] }`

    result := []byte(str)
    err := json.Unmarshal(result, &response)
    fmt.Println(err)
}
func main() {
    var res GetLoadResponse
    Get(&res)
    //Get(res) // Will not be wrong, but res is null
    fmt.Println(res)
}

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更改为:

type LoadMeter struct {
      MeterID string              `json:"meter_id"`
      DataSet []LoadDataSet       `json:"data_set"`
}
英文:

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

type LoadMeter struct {                                                                          
      MeterID string              `json:"meter_id"`
      DataSet []LoadDataSet       `json:"data_set"`
}   

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:

确定