使用数组中包含的[int]字符串映射对JSON进行解组
我试图将下面的JSON解组为一个结构,但在那里我无法用[[int,string]]转换值字段的内容 这就是我到目前为止所做的:使用数组中包含的[int]字符串映射对JSON进行解组,json,go,Json,Go,我试图将下面的JSON解组为一个结构,但在那里我无法用[[int,string]]转换值字段的内容 这就是我到目前为止所做的: type Response struct { Metric struct { Name string `json:"name,omitempty"` Appname string `json:"appname,omitempty"` } `json:"metr
type Response struct {
Metric struct {
Name string `json:"name,omitempty"`
Appname string `json:"appname,omitempty"`
} `json:"metric,omitempty"`
Values []map[int]string `json:"values,omitempty"`
}
JSON文件:
{
"metric":{
"name":"x444",
"appname":"cc-14-471s6"
},
"values":[
[
1508315264,
"0.0012116165566900816"
],
[
1508315274,
"0.0011871631158857396"
]
]
}
您显示的数据应解组到:
type Response struct {
Metric struct {
Name string `json:"name,omitempty"`
Appname string `json:"appname,omitempty"`
} `json:"metric,omitempty"`
Values [][]interface{} `json:"values,omitempty"`
}
如果要将其传输到映射实现json.Unmarshaller接口-
您可以选择以下内容:
type Item struct {
Key int
Val string
}
func(item *Item) UnmarshalJSON([]byte) error {
// TODO: implement
}
type Response struct {
Metric struct {
Name string `json:"name,omitempty"`
Appname string `json:"appname,omitempty"`
} `json:"metric,omitempty"`
Values []Item `json:"values,omitempty"`
}