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 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致