dsvcqvp139098 2016-11-10 03:53
浏览 97
已采纳

如何使用Golang func json解组此复杂的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:

enter image description here

  • 写回答

1条回答 默认 最新

  • dongxi1879 2016-11-14 06:41
    关注

    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"`
    }   
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?