亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

可以在 Go 中獲取 JSON 的值

可以在 Go 中獲取 JSON 的值

Go
藍山帝景 2023-05-08 18:06:04
我是 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 mainimport (    "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 回答

?
喵喔喔

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)

                }

            }

        }

    }

}


查看完整回答
反對 回復 2023-05-08
?
慕妹3146593

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)

}



查看完整回答
反對 回復 2023-05-08
  • 2 回答
  • 0 關注
  • 304 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號