代码之家  ›  专栏  ›  技术社区  ›  pcampana Helge

可以在go中获取json的值

  •  -1
  • pcampana Helge  · 技术社区  · 6 年前

    我刚加入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

    这是我的密码。我可以得到结果,但是我不能得到系列。

    这是我的代码:

    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"].(map[string]interface{})["series"]) 但问题是地图在一个数组中,我不知道如何解决它。

    2 回复  |  直到 6 年前
        1
  •  0
  •   nightfury1204    6 年前

    使用接口和映射

    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)
                    }
                }
            }
        }
    }
    
        2
  •  -1
  •   pcampana Helge    6 年前

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

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

    我用过这个 web 自动生成提供JSON结构的结构

    推荐文章