我有一个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 ..)
我该怎么办?
答案 0 :(得分:0)
您不仅需要解析输入,还需要收集IncidentID
s
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)
答案 1 :(得分:0)
我建议查看jsonpath。可能是您想要进行此类数据提取的库:https://pypi.python.org/pypi/jsonpath-ng/1.4.2
这是下一代链接。我相信它也有基础版本。
答案 2 :(得分:0)
已解决:
response = urllib2.urlopen(request)
含量= response.read()
data = json.loads(content)ids = []表示数据['content']中的事件: 打印(入射[ '事件'] [ 'IncidentID'])
谢谢大家的帮助
答案 3 :(得分:0)
import json
json_data = json.load(open("xyz.json", "r"))
final_list = [x["Incident"]["IncidentID"] for x in json_data["content"] if "Incident" in x and "IncidentID" in x["Incident"]]
for data in final_list: print data
答案 4 :(得分:0)
如果你想用理解来做 -
>>> test_dict = {"@count": 476, "@start": 1, "@totalcount": 476, "Messages": [],
"ResourceName": "Incident", "ReturnCode": 0, "content": [
{"Incident": {"IncidentID": "I08000003"}},
{"Incident": {"IncidentID": "I08000157"}},
{"Incident": {"IncidentID": "I08000274"}}]}
>>> [incident
for incident in row['Incident'].values()
for row in test_dict['content']]
['I08000274', 'I08000274', 'I08000274']