gpt4 book ai didi

json - 解码没有键的嵌套 json

转载 作者:行者123 更新时间:2023-12-01 22:27:09 25 4
gpt4 key购买 nike

我无法转换以下 json在 Golang 的一个结构中,来自 Kraken接口(interface):

{
"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 数据,我能够检索包含 asks 的接口(interface)和 bids列表。

// 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”的副本关闭。但是,如果您阅读这些问题,我不会处理两个不同的结构......我正在处理一个包含我之前不知道的 key 的 json。我也无法编码 BitfinexOrder

最佳答案

从@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)
}
}
}

关于json - 解码没有键的嵌套 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59338416/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com