为了账号安全,请及时绑定邮箱和手机立即绑定

解码仅存在于数组之外的 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 关注
  • 170 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号