1 回答
TA贡献1943条经验 获得超7个赞
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)
}
}
}
- 1 回答
- 0 关注
- 115 浏览
添加回答
举报