如何使用python3从大型Json数据中查找所有“名称”参数

时间:2019-06-24 06:19:16

标签: python json

如何使用Python3从大型JSON文件中提取所有名称。

with open('out.json', 'r') as f:
    data = f.read()

在此之后,我在这里打开JSON文件

a = json.dumps(data)
b= json.loads(a)
print (b) 

这是我来自JSON文件的数据。

{"data": [
{"errorCode":"E0000011","errorSummary":"Invalid token provided","errorLink":"E0000011","errorId":"oaeZ3PywqdMRWSQuA9_KML-ow","errorCauses":[]},
{"errorCode":"E0000011","errorSummary":"Invalid token provided","errorLink":"E0000011","errorId":"oaet_rFPO5bSkuEGKNI9a5vgQ","errorCauses":[]},
{"errorCode":"E0000011","errorSummary":"Invalid token provided","errorLink":"E0000011","errorId":"oaejsPt3fprRCOiYx-p7mbu5g","errorCauses":[]}]}

我需要这样的输出

{"oaeZ3PywqdMRWSQuA9_KML-ow","oaet_rFPO5bSkuEGKNI9a5vgQ","oaejsPt3fprRCOiYx-p7mbu5g"}

我想要所有errorId。

3 个答案:

答案 0 :(得分:2)

尝试这样:

Tweet

答案 1 :(得分:1)

如果要获取或处理JSON数据,则必须先加载JSON。

下面是代码示例

from json import loads
with open('out.json', 'r') as f:
    data = f.read()
    load = loads(data)

    names = [i['name'] for i in data['movie']['people']['actors']]

或者您可以将names = [i['name'] for i in data['movie']['people']['actors']]更改为Vikas P answers

答案 2 :(得分:1)

尝试将json模块用于上述内容。

import json
with open('path_to_file/data.json') as f:
   data = json.load(f)

actor_names = { names['name'] for names in data['movie']['people']['actors'] }