在没有密钥的情况下解组嵌套 json
Unmarshal a nested json without a key
我无法在 Golang 的结构中转换以下 json
,从 Kraken
API 收到:
{
"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
和 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" 的副本关闭。但是,如果您阅读这些问题,我不会处理两个不同的结构......我正在处理一个 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)
}
}
}
我无法在 Golang 的结构中转换以下 json
,从 Kraken
API 收到:
{
"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
和 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" 的副本关闭。但是,如果您阅读这些问题,我不会处理两个不同的结构......我正在处理一个 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)
}
}
}