将dict解析为dict python

2024-09-25 00:34:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个JSON文件,如下所示:

{"@count": 476, "@start": 1, "@totalcount": 476, "Messages": [],   
 "ResourceName": "Incident",   "ReturnCode": 0, "content": [
 {"Incident": {"IncidentID": "I08000003"}},
 {"Incident": {"IncidentID": "I08000157"}},
 {"Incident": {"IncidentID": "I08000274"}} }

当我尝试的时候

json_obj = json.load(response) for i in json_obj['content']:

我得到所有的行,但我只想得到数字(I08000003,I08000157…)

我该怎么办?你知道吗


Tags: 文件jsonobjcountcontentstartmessagesincident
3条回答

您不仅需要解析输入,还需要收集IncidentID

import json

content = #your json file content as a string
data = json.loads(content)
ids = []
for incident in data['content']:
    ids.push(incident['Incident']['IncidentID'])
print(ids)

已解决:

response = urllib2.urlopen(request)

content=response.read()

data = json.loads(content) ids = [] for incident in data['content']: print(incident['Incident']['IncidentID'])

谢谢大家的帮助

我建议你看看jsonpath。可能是用于这种数据提取的库:https://pypi.python.org/pypi/jsonpath-ng/1.4.2

这是下一代的联系。我相信它也有一个基本版本。你知道吗

相关问题 更多 >