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

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

解組沒有鍵的嵌套 json

解組沒有鍵的嵌套 json

Go
慕尼黑5688855 2022-04-26 10:45:18
我無法在從APIjson接收的 Golang 結(jié)構(gòu)中轉(zhuǎn)換以下內(nèi)容:Kraken{    "error": [],    "result": {        "LINKUSD": {            "asks": [                ["2.049720", "183.556", 1576323009],                ["2.049750", "555.125", 1576323009],                ["2.049760", "393.580", 1576323008],                ["2.049980", "206.514", 1576322995]            ],            "bids": [                ["2.043800", "20.691", 1576322350],                ["2.039080", "755.396", 1576323007],                ["2.036960", "214.621", 1576323006],                ["2.036930", "700.792", 1576322987]            ]        }    }}使用json-to-go,他給了我以下結(jié)構(gòu):type AutoGenerated struct {    Error  []interface{} `json:"error"`    Result struct {        LINKUSD struct {            Asks [][]interface{} `json:"asks"`            Bids [][]interface{} `json:"bids"`        } `json:"LINKUSD"`    } `json:"result"`}顯然,我無法LINKUSD對每個貨幣對都會改變的原因進行硬編碼。我創(chuàng)建了兩個結(jié)構(gòu)來完成任務(wù),但我無法將結(jié)果轉(zhuǎn)換為結(jié)構(gòu)。type BitfinexOrderBook struct {    Pair string          `json:"pair"`    Asks []BitfinexOrder `json:"asks"`    Bids []BitfinexOrder `json:"bids"`}type BitfinexOrder struct {    Price     string    Volume    string    Timestamp time.Time}我的第一次嘗試是使用反射。閱讀上面發(fā)布的 JSON 數(shù)據(jù),我能夠檢索包含asksandbids列表的接口。// Just used as a divisorconst div string = " ---------------------------------"func test(data []byte) error {    var err error    // Creating the maps for the JSON data    m := map[string]interface{}{}    // Parsing/Unmarshalling the json readed from the file    err = json.Unmarshal(data, &m)    if err != nil {        log.Println("Error unmarshalling data: " + err.Error())        return err    }
查看完整描述

1 回答

?
楊__羊羊

TA貢獻1943條經(jīng)驗 獲得超7個贊

package main


import (

    "fmt"

    "time"

    "encoding/json"

)


var data = []byte(`{

    "error": [],

    "result": {

        "LINKUSD": {

            "asks": [

                ["2.049720", "183.556", 1576323009],

                ["2.049750", "555.125", 1576323009],

                ["2.049760", "393.580", 1576323008],

                ["2.049980", "206.514", 1576322995]

            ],

            "bids": [

                ["2.043800", "20.691", 1576322350],

                ["2.039080", "755.396", 1576323007],

                ["2.036960", "214.621", 1576323006],

                ["2.036930", "700.792", 1576322987]

            ]

        }

    }

}`)


type Response struct {

    Error  []interface{}          `json:"error"`

    Result map[string]Order `json:"result"`

}


type Order struct {

    Asks []BitfinexOrder `json:"asks"`

    Bids []BitfinexOrder `json:"bids"`

}


type BitfinexOrder struct {

    Price     string

    Volume    string

    Timestamp time.Time

}


// UnmarshalJSON decode a BifinexOrder.

func (b *BitfinexOrder) UnmarshalJSON(data []byte) error {

    var packedData []json.Number

    err := json.Unmarshal(data, &packedData)

    if err != nil {

        return err

    }

    b.Price = packedData[0].String()

    b.Volume = packedData[1].String()

    t, err := packedData[2].Int64()

    if err != nil {

        return err

    }

    b.Timestamp = time.Unix(t, 0)

    return nil

}


func main() {

    res := &Response{}

    if err := json.Unmarshal(data, res); err != nil {

        panic(err)

    }


    for key, value := range res.Result {

        fmt.Println(key)

        for i, ask := range value.Asks {

            fmt.Printf("Asks[%d] = %#v\n", i, ask)

        }

        for i, bid := range value.Bids {

            fmt.Printf("Bids[%d] = %#v\n", i, bid)

        }

    }

}


查看完整回答
反對 回復(fù) 2022-04-26
  • 1 回答
  • 0 關(guān)注
  • 139 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

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