第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

使用 golang 的 encoding/json 讀取嵌套的 json 數(shù)據(jù)

使用 golang 的 encoding/json 讀取嵌套的 json 數(shù)據(jù)

Go
慕田峪9158850 2021-09-21 16:24:17
我無法為我的結(jié)構(gòu)獲取正確的定義來捕獲json保存在變量中的嵌套數(shù)據(jù)。我的代碼片段如下:package mainimport "fmt"import "encoding/json"type Data struct {    P string `json:"ports"`    Ports struct {         Portnums []int    }    Protocols []string `json:"protocols"`}func main() {        y := `{                "ports": {            "udp": [                1,                 30            ],             "tcp": [                100,                 1023            ]            },             "protocols": [            "tcp",             "udp"            ]    }`    var data Data    e := json.Unmarshal([]byte(y), &data)    if e == nil {        fmt.Println(data)    } else {        fmt.Println("Failed:", e)    }}$ go run foo.go Failed: json: cannot unmarshal object into Go value of type string
查看完整描述

1 回答

?
斯蒂芬大帝

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超8個(gè)贊

這對(duì)我有用(請(qǐng)參閱上面對(duì)您的問題的評(píng)論) GoPlay


type Data struct {

    Ports struct {

        Tcp []float64 `json:"tcp"`

        Udp []float64 `json:"udp"`

    } `json:"ports"`

    Protocols []string `json:"protocols"`

}


func main() {

    y := `{

                "ports": {

            "udp": [

                1, 

                30

            ], 

            "tcp": [

                100, 

                1023

            ]

            }, 

            "protocols": [

            "tcp", 

            "udp"

            ]

    }`

    d := Data{}

    err := json.Unmarshal([]byte(y), &d)

    if err != nil {

        fmt.Println("Error:", err.Error())

    } else {

        fmt.Printf("%#+v", d)

    }


}

輸出


main.Data{

    Ports:struct { 

        Tcp []float64 "json:\"tcp\"";

        Udp []float64 "json:\"udp\"" 

    }{

        Tcp:[]float64{100, 1023},

        Udp:[]float64{1, 30}

    },

    Protocols:[]string{"tcp", "udp"}

}


查看完整回答
反對(duì) 回復(fù) 2021-09-21
  • 1 回答
  • 0 關(guān)注
  • 362 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)