我有一个Map如下
{
"status": "success",
"data": {
"resultType": "vector",
"result": [{
"metric": {
"__name__": "up",
"alias": "xxxxx",
"instance": "xxxxx",
"job": "consul-prometheus"
},
"value": [1562051593.673, "1"]
}, {
"metric": {
"__name__": "up",
"alias": "xxxx",
"instance": "xxxxx",
"job": "consul-prometheus"
},
"value": [1562051593.673, "1"]
}]
}
}
我想取value里面的每一个值与第二个值,如何取
代码只能取到result就不会了
package main
import (
"encoding/json"
"fmt"
)
func main() {
json_strnew := `
{
"status": "success",
"data": {
"resultType": "vector",
"result": [{
"metric": {
"__name__": "up",
"alias": "xxxxx",
"instance": "xxxxx",
"job": "consul-prometheus"
},
"value": [1562051593.673, "1"]
}, {
"metric": {
"__name__": "up",
"alias": "xxxx",
"instance": "xxxxx",
"job": "consul-prometheus"
},
"value": [1562051593.673, "1"]
}]
}
}
`
m := make(map[string]interface{})
err := json.Unmarshal([]byte(json_strnew), &m)
fmt.Println(err)
if err != nil {
fmt.Println(err)
} else {
for k, v := range m["data"].(map[string]interface{}) {
fmt.Printf("%s:%s;\n", k, v)
fmt.Println(v)
//for kk, vv := range v {
// fmt.Printf("%s=%s;\n", kk, vv)
//}
}
}
}
有疑问加站长微信联系(非本文作者)