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

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

解碼僅存在于數組之外的 JSON 響應

解碼僅存在于數組之外的 JSON 響應

Go
慕虎7371278 2023-02-21 12:49:50
我在解碼 JSON 響應時遇到問題。我已經嘗試解決這個問題幾個星期,但無法在線找到有效的解決方案。這是我得到響應的 Go 代碼:package mainimport (    "fmt"    "time"    "strconv"    "encoding/json"    "net/http"    "io")const (    binanceUrl_0 = "https://api.binance.com"    binanceUrl_1 = "https://api1.binance.com"    binanceUrl_2 = "https://api2.binance.com"    binanceUrl_3 = "https://api3.binance.com"    //select which url to use    binanceUrl = binanceUrl_0    binance_GetServerTime   = binanceUrl + "/api/v3/time"    binance_Ping            = binanceUrl + "/api/v3/ping"    binance_GetExhangeInfo  = binanceUrl + "/api/v3/exchangeInfo"    binance_GetExhangeInfo_Symbol   = binanceUrl + "/api/v3/exchangeInfo?symbol=BNBBTC"    binance_GetKlineData    = binanceUrl + "/api/v1/klines")type Binance_klines struct {    OpenTime        int64           open            float32         high            float32         low             float32         close           float32         volume          float32         CloseTime        int64          QuoteVolume      float32        NumTrades        int64          TakerBaseVolume  float32        TakerQuoteVolume float32    }這是我得到的響應(從字節轉換為字符串):[[1664277480000,"20980.42000000","20984.06000000","20966.57000000","20970.14000000","6.10441000",1664277539999,"128041.93403330",142,"2.97844000","62486.29173860","0"],[1664277540000,"20969.14000000","20976.08000000","20955.69000000","20970.15000000","3.17365000",1664277599999,"66548.64583140",88,"2.39827000","50292.47196580","0"],[1664277600000,"20970.15000000","20970.15000000","20970.15000000","20970.15000000","0.00000000",1664277659999,"0.00000000",0,"0.00000000","0.00000000","0"]]我的問題是,我做錯了什么?當我使用像https://mholt.github.io/json-to-go/這樣的工具時,它要我制作一個 [][]interface{}。但是在我的 for 循環中,您可以看到它打印了一個(在我看來)一個有效的:[]接口{},但我無法將它轉換為 Binance_Klines 類型的結構。此行有問題嗎:kline = (to_parse).(Binance_klines)或者我只是誤會了什么?我需要更改什么才能使用類型斷言?還是立即將其解碼為正確的結構?
查看完整描述

2 回答

?
躍然一笑

TA貢獻1826條經驗 獲得超6個贊

你不能強制轉換[]interface{}為Binance_klines. 所以kline = (to_parse).(Binance_klines)失敗了。你必須自己寫翻譯。


返回的數據是一個二維數組。這是您格式化的 json 負載。string類型是和的混合float64,因此 Go 使用它interface{}來存儲值。


[

   [

      1664277480000,

      "20980.42000000",

      "20984.06000000",

      "20966.57000000",

      "20970.14000000",

      "6.10441000",

      1664277539999,

      "128041.93403330",

      142,

      "2.97844000",

      "62486.29173860",

      "0"

   ],

   [

      1664277540000,

      "20969.14000000",

      "20976.08000000",

      "20955.69000000",

      "20970.15000000",

      "3.17365000",

      1664277599999,

      "66548.64583140",

      88,

      "2.39827000",

      "50292.47196580",

      "0"

   ],

   [

      1664277600000,

      "20970.15000000",

      "20970.15000000",

      "20970.15000000",

      "20970.15000000",

      "0.00000000",

      1664277659999,

      "0.00000000",

      0,

      "0.00000000",

      "0.00000000",

      "0"

   ]

]

json 解碼器無法將其轉換為您的Binance_klines結構。但是您可以自己覆蓋默認的解組行為。


首先,我為有時引用的數字制作了一個類型,有時不引用。


type BinanceNumber string


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

    *b = BinanceNumber(strings.Trim(string(data), "\""))

    return nil

}


func (b BinanceNumber) Float64() float64 {

    f, err := strconv.ParseFloat(string(b), 64)

    if err != nil {

        panic(err)

    }

    return f

}


func (b BinanceNumber) Int64() int64 {

    i, err := strconv.ParseInt(string(b), 10, 64)

    if err != nil {

        panic(err)

    }

    return i

}

然后你覆蓋Binance_klines解組。


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

    var array []BinanceNumber

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

    if err != nil {

        return err

    }


    b.OpenTime = array[0].Int64()

    b.Open = float32(array[1].Float64())

    b.High = float32(array[2].Float64())

    b.Low = float32(array[3].Float64())

    b.Close = float32(array[4].Float64())

    b.Volume = float32(array[5].Float64())

    b.CloseTime = array[6].Int64()

    b.QuoteVolume = float32(array[7].Float64())

    b.NumTrades = array[8].Int64()

    b.TakerBaseVolume = float32(array[9].Float64())

    b.TakerQuoteVolume = float32(array[10].Float64())


    return nil

}

將它們放在一起: https: //go.dev/play/p/SGGbWEUFxJr。


查看完整回答
反對 回復 2023-02-21
?
青春有我

TA貢獻1784條經驗 獲得超8個贊

這部分:


var kline Binance_klines

kline = (to_parse).(Binance_klines)

需要成為


var kline Binance_klines

kline.OpenTimer = to_parse[0].(int64)

kline.open = strconv.ParseFloat(to_parse[1].(string), 64)

...

您沒有收到您的Binance_klines結構的 json 表示形式,而是任何部分(混合或數字和字符串)。


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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