英文:
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数据的图像:
英文:
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:
答案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"`
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论