【发布时间】:2013-03-27 02:24:39
【问题描述】:
我需要从包含大约 70.000 个(子)键/对象的 JSON 格式文本中获取主键(设备) 它看起来像这样:
{
"1":{...........}
"4":{...........}
"9":{...........}
}
我需要得到“1”、“4”和“9”。但是我现在这样做的方式需要大约 2 分钟来解析文本与
json = json.loads(response.text) #this takes so long!
devices = json.keys()
因为我在树莓派上运行它!
有没有更好的办法?
编辑: 我从运行在服务器上的 JSON API 接收数据:
http://.../ZWaveAPI/Run/devices #this is an array
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
【问题讨论】:
-
使用数据库,只在需要时才查询?
-
我无法更改我得到的数据...我收到一个包含许多键的文本,我需要获取主键...或者我获取数据的方式有可能? (见编辑)