我正在尝试研究此 JSON 响应的类型结构。 来自 CryptoWatch https://api.cryptowat.ch/markets/kraken/btcusd/ohlc?periods=60
{
"result": {
"60": [
[
1490733900,
1027.001,
1027.001,
1027,
1027,
0.024999999
],
[
1490733960,
1027,
1027,
1027,
1027,
12.61904
],
[
1490778360,
1037.749,
1037.749,
1037.749,
1037.749,
0.0052474597
]
]
},
"allowance": {
"cost": 1234,
"remaining": 456677
}
}
我已经尝试过 JSON-TO-GoLang-Struct 服务。
类型自动生成结构{ 结果结构{ Num60 []结构{ Num0 int
json:"0"
Num1 float64json:"1"
Num2 float64json:"2"
Num3 intjson:"3"
Num4 intjson:"4"
Num5 float64json:"5"
}json:"60"
}json:"结果"
津贴结构{ 成本 intjson:"cost"
剩余 intjson:"remaining"
}json:"allowance"
代码格式似乎损坏了:/
当我尝试对其进行解码时,它似乎陷入了数组到结构的错误。
- 我不想为此结构使用 map+interface 方法。
最佳答案
给你。您也可以将结果作为 map[string][][]float64
。但我认为这更具可读性。
type Result [][]float64
type Response struct {
Result map[string]Result `json:"result"`
Allowance struct {
Cost int `json:"cost"`
Remaining int `json:"remaining"`
} `json:"allowance"`
}
关于json - JSON 响应结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43089138/