我正在努力创建用于解组以下json的数据结构:
{
"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 OrderBook struct {
Asks [][]interface{} `json:"asks"`
Bids [][]interface{} `json:"bids"`
}
但是我需要更严格的输入,所以我尝试了:
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
}
但是不幸的是我没有成功。
这是我用来解析Kraken API来检索订单的代码:
// loadKrakenOrderBook is delegated to load the data related to pairs info
func loadKrakenOrderBook(data []byte) (datastructure.BitfinexOrderBook, error) {
var err error
// Creating the maps for the JSON data
m := map[string]interface{}{}
var orderbook datastructure.BitfinexOrderBook
// Parsing/Unmarshalling JSON
err = json.Unmarshal(data, &m)
if err != nil {
zap.S().Debugw("Error unmarshalling data: " + err.Error())
return orderbook, err
}
a := reflect.ValueOf(m["result"])
if a.Kind() == reflect.Map {
key := a.MapKeys()[0]
log.Println("KEY: ", key)
strct := a.MapIndex(key)
log.Println("MAP: ", strct)
m, _ := strct.Interface().(map[string]interface{})
log.Println("M: ", m)
data, err := json.Marshal(m)
if err != nil {
zap.S().Warnw("Panic on key: ", key.String(), " ERR: "+err.Error())
return orderbook, err
}
log.Println("DATA: ", string(data))
err = json.Unmarshal(data, &orderbook)
if err != nil {
zap.S().Warnw("Panic on key: ", key.String(), " during unmarshal. ERR: "+err.Error())
return orderbook, err
}
return orderbook, nil
}
return orderbook, errors.New("UNABLE_PARSE_VALUE")
}
我用于测试的数据如下:
{
"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]
]
}
}
}
NOTE:我在输入中收到的数据是我发布的最新json,而不是bids
和asks
的数组。
我已经尝试整合@chmike提出的解决方案。不幸的是,需要进行一些预处理,因为数据是我发布的最新json。
因此,为了提取与asks
和bids
相关的json数据,我更改为以下代码。>
func order(data []byte) (datastructure.BitfinexOrderBook, error) { var err error // Creating the maps for the JSON data m := map[string]interface{}{} var orderbook datastructure.BitfinexOrderBook // var asks datastructure.BitfinexOrder // var bids datastructure.BitfinexOrder // Parsing/Unmarshalling JSON err = json.Unmarshal(data, &m) if err != nil { zap.S().Warn("Error unmarshalling data: " + err.Error()) return orderbook, err } // Extract the "result" json a := reflect.ValueOf(m["result"]) if a.Kind() == reflect.Map { key := a.MapKeys()[0] log.Println("KEY: ", key) log.Println() strct := a.MapIndex(key) log.Println("MAP: ", strct) m, _ := strct.Interface().(map[string]interface{}) log.Println("M: ", m) log.Println("Asks: ", m["asks"]) log.Println("Bids: ", m["bids"]) // Here i retrieve the asks array asks_data, err := json.Marshal(m["asks"]) log.Println("OK: ", err) log.Println("ASKS: ", string(asks_data)) var asks datastructure.BitfinexOrder // here i try to unmarshal the data into the struct asks, err = UnmarshalJSON(asks_data) log.Println(err) log.Println(asks) } return orderbook, errors.New("UNABLE_PARSE_VALUE") }
不幸的是,我收到以下错误:
json: cannot unmarshal array into Go value of type json.Number
为什么@ Volker,@ Peter和@Sourabh Choraria结束了这些问题?不是重复的,下面的数据结构不需要两个不同的结构,并且问题与两个不同的结构无关。而是与以下json相关:
{ "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的数据结构:{“ asks”:[[“ 2.049720”,“ 183.556”,1576323009],[“ 2.049750”,“ 555.125”,1576323009],[“ 2 ....
如@Flimzy所建议,您需要自定义Unmarshaler
。这里是。
虽然您可以使用反射功能来破解自己的方法,甚至可以编写自己的解析器,但最有效的方法是实现json.Unmarshaler
。