小编典典

解封没有密钥的嵌套json

go

我无法json在通过KrakenAPI 接收的Golang结构中强制转换以下内容:

{
    "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,他给我以下结构:

type AutoGenerated struct {
    Error  []interface{} `json:"error"`
    Result struct {
        LINKUSD struct {
            Asks [][]interface{} `json:"asks"`
            Bids [][]interface{} `json:"bids"`
        } `json:"LINKUSD"`
    } `json:"result"`
}

显然,我无法LINKUSD对每种货币对都会改变的原因进行硬编码。

我已经创建了两个结构来完成任务,但是无法将结果强制转换为结构。

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
}

我的第一个尝试是使用反射。阅读上面发布的JSON数据,我能够检索包含asksbids列表的接口。

// Just used as a divisor
const 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
    }

    // Extract the "result" only
    a := reflect.ValueOf(m["result"])

    if a.Kind() == reflect.Map {
        key := a.MapKeys()[0] // Extract the key -> LINKUSD
        log.Println("KEY: ", key, div)
        strct := a.MapIndex(key) // Extract the value -> asks and bids array
        log.Println("MAP: ", strct, div)
        m, _ := strct.Interface().(map[string]interface{})
        log.Println("Asks: ", m["asks"], div) // This will print [[value, value, value] ...] related to asks
        log.Println("Bids: ", m["bids"], div) // This will print [[value, value, value] ...] related to bids

        // Parse the interface into a []byte
        asks_data, err := json.Marshal(m["asks"])
        log.Println("OK: ", err, div)
        log.Println("ASKS: ", string(asks_data), div)
        // Tried without array to (datastructure.BitfinexOrder)
        var asks []datastructure.BitfinexOrder
        err = json.Unmarshal(asks_data, &asks)
        log.Println("ERROR: ", err, div)
        log.Println("UNMARSHAL: ", asks, div)

    }
    return errors.New("UNABLE_PARSE_VALUE")
}

下面的两个打印m, _ := strct.Interface().(map[string]interface{})将显示以下interface类型的事实,由于类型的事实,我无法投射以下类似的数据:

[[2.049720 183.556 1.576323009e+09] [2.049750 555.125 1.576323009e+09] [2.049760 393.580 1.576323008e+09] [2.049980 206.514 1.576322995e+09]]

但是我无法解组数据。

所以我尝试了由提供的其他功能@chmike

// UnmarshalJSON decode a BifinexOrder.
func UnmarshalJSON(data []byte) (datastructure.BitfinexOrder, error) {
    var packedData []json.Number

    var order datastructure.BitfinexOrder
    err := json.Unmarshal(data, &packedData)
    if err != nil {
        return order, err
    }
    order.Price = packedData[0].String()
    order.Volume = packedData[1].String()
    t, err := packedData[2].Int64()
    if err != nil {
        return order, err
    }
    order.Timestamp = time.Unix(t, 0)
    return order, nil
}

但是我收到以下错误:

json: cannot unmarshal array into Go value of type json.Number

一些技巧?

注意我以前的问题已作为“解组切片中2个不同结构”的副本而关闭。
但是,如果您阅读了这些问题,那么我就不会处理两种不同的结构…我正在处理的json包含一个我以前不知道的键。我也无法封送BitfinexOrder


阅读 261

收藏
2020-07-02

共1个答案

小编典典

从@mkopriva和@chmike找到解决方案:

感谢你们!

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)
        }
    }
}
2020-07-02