如何使用 Golang func json.Unmarshal 解组这个复杂的 Json 数据?
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 值
游乐场:https://play.golang.org/p/ywFUu2MVNR
JSON 数据的图像:
您在 meter_set
元素中的 data_set
是 LoadDataSet
的数组。将您的 LoadMeter
更改为:
type LoadMeter struct {
MeterID string `json:"meter_id"`
DataSet []LoadDataSet `json:"data_set"`
}
我有这样的 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 值游乐场:https://play.golang.org/p/ywFUu2MVNR
JSON 数据的图像:
您在 meter_set
元素中的 data_set
是 LoadDataSet
的数组。将您的 LoadMeter
更改为:
type LoadMeter struct {
MeterID string `json:"meter_id"`
DataSet []LoadDataSet `json:"data_set"`
}