gpt4 book ai didi

没有解析的 Python JSON 键

转载 作者:行者123 更新时间:2023-11-28 21:59:55 25 4
gpt4 key购买 nike

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

{
"1":{...........}
"4":{...........}
"9":{...........}
}

我需要得到“1”、“4”和“9”。但是我现在这样做需要大约 2 分钟来解析文本

json = json.loads(response.text) #this takes so long!
devices = json.keys()

因为我在 Raspberry Pi 上运行它!

有没有更好的办法?

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

http://.../ZWaveAPI/Run/devices #this is an array

编辑 3:

最终工作代码:(运行 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

最佳答案

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

for prefix, event, value in parser:
if event == 'map_key':
print value

关于没有解析的 Python JSON 键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15820916/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com