1

我无法在从APIjson接收的 Golang 结构中转换以下内容: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,他给了我以下结构:

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 数据,我能够检索包含asksandbids列表的接口。

// 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

一些技巧?

注意我之前的问题已作为“Unmarshal 2 different structs in a slice”的副本关闭。但是,如果您阅读这些问题,我不会处理两个不同的结构......我正在处理一个包含我之前不知道的密钥的 json。我也无法编组 BitfinexOrder

4

1 回答 1

0

从@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)
        }
    }
}
于 2019-12-14T19:56:06.653 回答