json - JSON 响应结构

标签 json go struct

我正在尝试研究此 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
  }
}
  1. 我已经尝试过 JSON-TO-GoLang-Struct 服务。

    类型自动生成结构{ 结果结构{ Num60 []结构{ Num0 int json:"0" Num1 float64 json:"1" Num2 float64 json:"2" Num3 int json:"3" Num4 int json:"4" Num5 float64 json:"5" } json:"60" } json:"结果" 津贴结构{ 成本 int json:"cost" 剩余 int json:"remaining" } json:"allowance"

    代码格式似乎损坏了:/

当我尝试对其进行解码时,它似乎陷入了数组到结构的错误。

  1. 我不想为此结构使用 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/

相关文章:

java - 最佳实践 : Save Empty Fields as null or omit Field Completely and Manage Missing Fields in Code?

jquery - 使用 JSON 数据自动完成 jQuery

戈朗 : readsym out of sync

algorithm - google go 中的可重用优先级队列实现

c - typedef'ed struct c,如何访问在typedef中声明为指针的结构实例?

c# - 我应该使用结构而不是类来在 C# 中保存仅字符串数据吗?

php - 如何从 PHP 数组中获取 `json_encode()` 键?

php json_encode mysql 结果以特定方式

go - 与 channel 的双向通讯

c++ - C++ 中的命名冲突 : How to access a struct member called "class"