如何在没有子数据的情况下获得价值?

时间:2019-05-20 01:00:42

标签: 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"
      }
    }
  }
}

如何在没有其他条件的情况下抓取论文?

2 个答案:

答案 0 :(得分:2)

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'])
>>>

答案 1 :(得分:1)

实际上,使用regex to parse your json is not the good approach,您需要做的是将json加载到字典对象中后,通过dict.keys()访问所有键。

import json

inStr = """{
  "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(inStr)
print(data['result']['open'].keys())