python - 如何在没有子数据的情况下获取值(value)?

标签 python json parsing

尝试从我的 json 中获取这些数据:

O2FDSFDSN47U-BFSDFSDWO3-TTFSDFS245H
OIBKDFSDFS2B-K6A2FSDFSDO-2GPFSDFSNES
OSZZEE-FDSDQUNAZ-OHDSQDQS2NDS

使用 print(data['result']['open']) : 但返回 id 之后的所有数据。

这些id还在动,我猜不出来。 使用正则表达式似乎不是一个好主意

没有别的,但是当使用我的 python 解析器时,我总是得到子数据。

{
  "error": [],
  "result": {
    "open": {
      "O2FDSFDSN47U-BFSDFSDWO3-TTFSDFS245H": {
        "refid": null,
        "userref": 0,
        "status": "open",
        "starttm": 0,
        "expiretm": 0,
        "descr": {
          "ordertype": "limit",
          "price": "3",
          "price2": "0",
          "leverage": "none",
          "close": ""
        },
        "vol": "1.00000000",
        "vol_exec": "0.00000000",
        "cost": "0.00000",
        "fee": "0.00000",
        "price": "0.00000",
        "stopprice": "0.00000",
        "limitprice": "0.00000",
        "misc": "",
        "oflags": "fcib"
      },
      "OIBKDFSDFS2B-K6A2FSDFSDO-2GPFSDFSNES": {
        "refid": null,
        "userref": 0,
        "status": "open",
        "starttm": 0,
        "expiretm": 0,
        "descr": {
          "ordertype": "limit",
          "price": "1.5",
          "price2": "0",
          "leverage": "none",
          "close": ""
        },
        "vol": "2.00000000",
        "vol_exec": "0.00000000",
        "cost": "0.00000",
        "fee": "0.00000",
        "price": "0.00000",
        "stopprice": "0.00000",
        "limitprice": "0.00000",
        "misc": "",
        "oflags": "fciq"
      },
      "OSZZEE-FDSDQUNAZ-OHDSQDQS2NDS": {
        "refid": null,
        "userref": 0,
        "status": "open",
        "starttm": 0,
        "expiretm": 0,
        "descr": {
          "ordertype": "limit",
          "price": "9",
          "price2": "0",
          "leverage": "none",
          "close": ""
        },
        "vol": "1.00000000",
        "vol_exec": "0.00000000",
        "cost": "0.00000",
        "fee": "0.00000",
        "price": "0.00000",
        "stopprice": "0.00000",
        "limitprice": "0.00000",
        "misc": "",
        "oflags": "fciq"
      }
    }
  }
}

如何一举拿下论文?

最佳答案

import json
s = '''{
  "error": [],
  "result": {
    "open": {
      "O2FDSFDSN47U-BFSDFSDWO3-TTFSDFS245H": {
        "refid": null,
        "userref": 0,
        "status": "open",
        "starttm": 0,
        "expiretm": 0,
        "descr": {
          "ordertype": "limit",
          "price": "3",
          "price2": "0",
          "leverage": "none",
          "close": ""
        },
        "vol": "1.00000000",
        "vol_exec": "0.00000000",
        "cost": "0.00000",
        "fee": "0.00000",
        "price": "0.00000",
        "stopprice": "0.00000",
        "limitprice": "0.00000",
        "misc": "",
        "oflags": "fcib"
      },
      "OIBKDFSDFS2B-K6A2FSDFSDO-2GPFSDFSNES": {
        "refid": null,
        "userref": 0,
        "status": "open",
        "starttm": 0,
        "expiretm": 0,
        "descr": {
          "ordertype": "limit",
          "price": "1.5",
          "price2": "0",
          "leverage": "none",
          "close": ""
        },
        "vol": "2.00000000",
        "vol_exec": "0.00000000",
        "cost": "0.00000",
        "fee": "0.00000",
        "price": "0.00000",
        "stopprice": "0.00000",
        "limitprice": "0.00000",
        "misc": "",
        "oflags": "fciq"
      },
      "OSZZEE-FDSDQUNAZ-OHDSQDQS2NDS": {
        "refid": null,
        "userref": 0,
        "status": "open",
        "starttm": 0,
        "expiretm": 0,
        "descr": {
          "ordertype": "limit",
          "price": "9",
          "price2": "0",
          "leverage": "none",
          "close": ""
        },
        "vol": "1.00000000",
        "vol_exec": "0.00000000",
        "cost": "0.00000",
        "fee": "0.00000",
        "price": "0.00000",
        "stopprice": "0.00000",
        "limitprice": "0.00000",
        "misc": "",
        "oflags": "fciq"
      }
    }
  }
}'''

data = json.loads(s)

>>> data['result']['open'].keys()
dict_keys(['O2FDSFDSN47U-BFSDFSDWO3-TTFSDFS245H', 'OIBKDFSDFS2B-K6A2FSDFSDO-2GPFSDFSNES', 'OSZZEE-FDSDQUNAZ-OHDSQDQS2NDS'])
>>>

关于python - 如何在没有子数据的情况下获取值(value)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56213235/

相关文章:

python - 在 Python 中使用列表理解映射嵌套列表?

ios - 需要过滤来自 URL 的结果

java - Mime4j:DefaultMessageBuilder 无法解析 mbox 内容

javascript - 使用javascript解析JSON并获取数组中的特定值

python - 如何使用 Python 和 ctypes 在 Windows 上读取或写入 as h r i 文件属性?

python - python 列表的元素级 'and'?

json - 使用 golang 从 http 网络服务器中提取特定的和最新的数据

java - 通过 JSON 将 DOM 事件转为 java

json - Node.js 将 XML 转换为 JSON 并将其返回给前端

python - 如何为无参数方法自动生成测试用例?