使用Python从Json代码中提取值
我试图通过在不同的行中显示每个设备,以这种格式从以下json代码中提取值。有什么办法吗?提前谢谢使用Python从Json代码中提取值,python,json,list,Python,Json,List,我试图通过在不同的行中显示每个设备,以这种格式从以下json代码中提取值。有什么办法吗?提前谢谢 device1 : 232323 Completed device2 : 345848 Completed etc 我尝试了以下方法,得到了以下输出: [{'cid': 631084346, 'data': [[{'text': 'device1'}], [{'text': '732536:Completed.'}], [{'text': '1'}]], 'id': 1750501182}, {'
device1 : 232323 Completed
device2 : 345848 Completed etc
我尝试了以下方法,得到了以下输出:
[{'cid': 631084346, 'data': [[{'text': 'device1'}], [{'text': '732536:Completed.'}], [{'text': '1'}]], 'id': 1750501182}, {'cid': 1891684718, 'data': [[{'text': 'device2'}], [{'text': '732536:Completed.'}], [{'text': '1'}]], 'id': 2218910703}
使用的Json示例
{
"data": {
"max_available_age": "",
"now": "2020/01/09 22:43:58 GMT-0000",
"result_sets": [{
"age": 0,
"archived_question_id": 0,
"columns": [{
"hash": 3409330187,
"name": "Computer Name",
"type": 1
},
{
"hash": 1792443391,
"name": "Action Statuses",
"type": 1
},
{
"hash": 0,
"name": "Count",
"type": 3
}
],
"error_count": 0,
"estimated_total": 129,
"expire_seconds": 3660,
"filtered_row_count": 11,
"filtered_row_count_machines": 11,
"id": 2880628,
"issue_seconds": 0,
"item_count": 11,
"mr_passed": 129,
"mr_tested": 129,
"no_results_count": 0,
"passed": 128,
"question_id": 2880628,
"report_count": 233,
"row_count": 11,
"row_count_machines": 11,
"rows": [{
"cid": 631084346,
"data": [
[{
"text": "device1"
}],
[{
"text": "732536:Completed."
}],
[{
"text": "1"
}]
],
"id": 1750501182
},
{
"cid": 1891684718,
"data": [
[{
"text": "device2"
}],
[{
"text": "732536:Completed."
}],
[{
"text": "1"
}]
],
"id": 2218910703
}
api\u readable['data']['result\u sets'][0]['rows']
是一个列表,您只需在其上循环并打印所需的项目即可
for row in api_readable['data']['result_sets'][0]['rows']:
print(f"{row['data'][0][0]['text']} : {row['data'][1][0]['text']}");
+提议的复制品。@tevemadar会怎么做?他们已经解析了他们的JSON,他们的问题与JSON无关。@StefanPochmann你可能是对的,但我没有看到实际的解析,我看到了一个有注释的regexp。@tevemadar它显然已经解析了,并且在
api_readable
变量中。它似乎不起作用。我现在收到这个错误:print(f{row['data'][0]['text']}:{row['data'][1]['text']}”);类型错误:列表索引必须是整数或切片,而不是strf出于某种原因,data
中的每个字典都嵌套在一个只包含一个元素的额外列表中。我添加了额外的索引。
for row in api_readable['data']['result_sets'][0]['rows']:
print(f"{row['data'][0][0]['text']} : {row['data'][1][0]['text']}");