2 回答

TA貢獻1735條經驗 獲得超5個贊
使用界面和地圖
package main
import (
"encoding/json"
"fmt"
)
func main() {
byteValue := []byte(`{"results":[{"statement_id":0,"series":[{"name":"cpu/node_utilization","columns":["time","distinct"],"values":[[10,1],[11,3],[13,5]]}]}]}`)
var all_data map[string][]interface{}
json.Unmarshal([]byte(byteValue), &all_data)
fmt.Println("result:", all_data["results"])
for _, r := range all_data["results"] {
s := r.(map[string]interface{})
fmt.Println("series:", s["series"])
w := s["series"].([]interface{})
for _, x := range w {
y := x.(map[string]interface{})
fmt.Println(y)
z := y["values"].([]interface{})
fmt.Println("values:", z)
for _, v := range z {
u := v.([]interface{})
fmt.Println(u)
for _, i := range u {
val := i.(float64)
fmt.Println(val)
}
}
}
}
}

TA貢獻1820條經驗 獲得超9個贊
我已經解決了定義結構的問題。
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
)
type AutoGenerated struct {
? ? Results []struct {
? ? ? ? StatementID int `json:"statement_id"`
? ? ? ? Series? ? ? []struct {
? ? ? ? ? ? Name? ? string? ?`json:"name"`
? ? ? ? ? ? Columns []string `json:"columns"`
? ? ? ? ? ? Values? [][]int? `json:"values"`
? ? ? ? } `json:"series"`
? ? } `json:"results"`
}
func main() {
? ? // Open our jsonFile
jsonFile, err := os.Open("example.json")
// if we os.Open returns an error then handle it
if err != nil {
? ? fmt.Println(err)
}
fmt.Println("Successfully Opened example.json")
// defer the closing of our jsonFile so that we can parse it later on
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
all_data := AutoGenerated{}
json.Unmarshal([]byte(byteValue), &all_data)
fmt.Println(all_data.Results[0].Series[0].Values)
}
- 2 回答
- 0 關注
- 304 瀏覽
添加回答
舉報