可以在Go中获取JSON的值

可以在Go中获取JSON的值,json,parsing,go,Json,Parsing,Go,我是围棋新手。我试图读取一个JSON文件并获取其中的一部分,然后使用获得的值进行操作。 我的JSON在example.JSON文件中: {"results":[{"statement_id":0,"series":[{"name":"cpu/node_utilization","columns":["time","distinct"],"values":[[10,1],[11,3],[13,5]]}]}]} 所以我想得到的是所有元素之和的“值”。在这种情况下:1+3+5 这是我的代码。我可以获

我是围棋新手。我试图读取一个JSON文件并获取其中的一部分,然后使用获得的值进行操作。 我的JSON在example.JSON文件中:

{"results":[{"statement_id":0,"series":[{"name":"cpu/node_utilization","columns":["time","distinct"],"values":[[10,1],[11,3],[13,5]]}]}]}
所以我想得到的是所有元素之和的“值”。在这种情况下:1+3+5

这是我的代码。我可以获得结果,但我无法获得系列

以下是我的代码:

package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "os"
)

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)
    var all_data map[string]interface{}
    json.Unmarshal([]byte(byteValue), &all_data)
    fmt.Println(all_data["results"])
}
我尝试过不同的解决方案,比如
all_data[“results”]。(映射[字符串]接口{})[“series”])

但问题是映射在一个数组中,我不知道如何解决它。

我已经解决了定义结构的问题

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)
}

我已经使用它来自动生成结构,使用接口和映射提供JSON结构

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)
                }
            }
        }
    }
}

对切片类型使用
[]
。例如,
all_data[“results”]。([]映射[string]接口{})
。那么在这种情况下应该如何?我正在尝试
data\u results:=all\u data[“results”]。([]map[string]interface{})
但我在尝试打印时出错:
panic:interface conversion:interface{}是[]interface{},而不是[]map[string]interface{}
我找到了一个使用Struct的解决方案,但我想看看如何使用interface解决它。