没有解析的Python JSON密钥

TeN*_*NoX 2 python parsing json

我需要从大约70.000(子)键/对象的JSON格式文本中获取主键(设备)它看起来像这样:

{
   "1":{...........}
   "4":{...........}
   "9":{...........}
}
Run Code Online (Sandbox Code Playgroud)

我需要得到"1","4"和"9".但是我现在的方式是解析文本需要大约2分钟

json = json.loads(response.text) #this takes so long!
devices = json.keys()
Run Code Online (Sandbox Code Playgroud)

因为我在Raspberry Pi上运行它!

有没有更好的办法?

编辑: 我收到服务器上运行的JSON API的数据:

http://.../ZWaveAPI/Run/devices #this is an array
Run Code Online (Sandbox Code Playgroud)

EDIT3:

最终工作代码:(运行2-5秒!:)

import ijson.backends.python as ijson
import urllib

parser = ijson.parse(urllib.urlopen("http://.../ZWaveAPI/Run/devices"))
list = []
for prefix,event,value in parser:
    if event == "map_key" and len(prefix) == 0:
        list.append(value)
return list
Run Code Online (Sandbox Code Playgroud)

Mar*_*ers 5

您可以使用面向流的迭代JSON解析器来完成它,但您需要单独安装它.试试看ijson,它会为遇到的每个JSON结构发出事件:

for prefix, event, value in parser:
    if event == 'map_key':
        print value
Run Code Online (Sandbox Code Playgroud)