go - 如何提取嵌套的 JSON 数据?

标签 go

我在 data.json 中有以下内容:

{  
    "table":"orderBook10",
    "action":"update",
    "data":[  
       {  
          "symbol":"XBTUSD",
          "bids":[  
             [  
                3996,
                49137
             ],
             [  
                3995.5,
                116
             ],
             [  
                3995,
                165
             ],
             [  
                3994.5,
                166
             ],
             [  
                3994,
                237
             ],
             [  
                3993.5,
                45
             ],
             [  
                3992,
                20064
             ],
             [  
                3991.5,
                209
             ],
             [  
                3991,
                134
             ],
             [  
                3990.5,
                2948
             ]
          ],
          "timestamp":"2019-03-23T00:34:40.505Z",
          "asks":[  
             [  
                3996.5,
                975
             ],
             [  
                3997,
                289
             ],
             [  
                3997.5,
                334
             ],
             [  
                3998,
                419
             ],
             [  
                3998.5,
                423
             ],
             [  
                3999,
                930
             ],
             [  
                3999.5,
                547
             ],
             [  
                4000,
                538
             ],
             [  
                4000.5,
                703
             ],
             [  
                4001,
                997
             ]
          ]
       }
    ]
 }

到目前为止,我的程序可以提取 data 字段:

package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
)

func main() {

    dat, err := ioutil.ReadFile("./data.json")
    if err != nil {
        panic(err)
    }

    var ob map[string]interface{}
    if err := json.Unmarshal(dat, &ob); err != nil {
        panic(err)
    }
    fmt.Println(ob["data"])
}

我现在想提取嵌套的“询问”字段。

我试过:

data := ob["data"]
asks := data["asks"].([][]int)

但这会导致我无法破译的语法错误。

如何将嵌套的 asks 字段分配给变量?

最佳答案

你的ob["data"] JSON结构如下:

([]interface {}) {
  (map[string]interface {}) {
    (string) "": (string) "",
    (string) "": ([]interface {}) {
      ([]interface {}) {
        (float64),
        (float64) 
      }
    },
    (string) "": (string) "",
    (string) "": ([]interface {}) {
      ([]interface {}) {
        (float64),
        (float64)
      }
    }
  }
}

因此您需要使用以下内容:

ob["data"].([]interface{})[0].(map[string]interface{})["asks"]

哪个:

  • ob["data"] 转换为 []interface{}
  • 使用第一个(在您的示例中是唯一的)元素 [0]
  • 将此元素转换为 map[string]interface{},并且
  • 然后提取“asks”

注意: 除非您确定,否则始终使用此形式 t, ok := i.(T) for type assertions :

If i holds a T, then t will be the underlying value and ok will be true.

If not, ok will be false and t will be the zero value of type T, and no panic occurs.

关于go - 如何提取嵌套的 JSON 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55309554/

相关文章:

go程序编译错误

go - 如何在另一个模块中使用 "GOPATH"之外的模块?

go - http中的Goroutines

http - Webhook 进程在另一个 goroutine 上运行

go - []byte ("") 默认容量是多少?

json - JSON 请求和响应的一般处理

mysql - 去-导入sql.a : not a package file

Golang tcp客户端服务器程序

go - 解码 JSON 对象并将值映射到 Go 中的结构

nginx - 无法使用 NginX/FastCGI 建立 Websocket 连接