【问题标题】:Extract values from Json code using Python [duplicate]使用Python从Json代码中提取值[重复]
【发布时间】:2020-01-09 22:57:27
【问题描述】:

我试图通过在不同的行中显示每个设备来以这种格式从以下 json 代码中提取值。关于如何做的任何想法?提前致谢

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}
api_readable = api_response.json()
    computer_name = str(api_readable['data']['result_sets'][0]['rows'])
   # computer_name = re.sub("[{},':]", "", computer_name)
    print(computer_name)

使用的示例 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
                    }

【问题讨论】:

  • @tevemadar 怎么样?他们已经解析了他们的 JSON,他们的问题与 JSON 无关。
  • @StefanPochmann 你可能是对的,但我没有看到实际的解析,我看到了一个带注释的正则表达式。
  • @tevemadar 它显然已经被解析并且在 api_readable 变量中。

标签: python json list


【解决方案1】:

api_readable['data']['result_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']}");

【讨论】:

  • 它似乎不起作用我现在收到此错误:
  • print(f"{row['data'][0]['text']} : {row['data'][1]['text']}"); TypeError: 列表索引必须是整数或切片,而不是 str
  • 出于某种原因,data 中的每个字典都嵌套在一个只有一个元素的额外列表中。我已经添加了额外的索引。
猜你喜欢
  • 2020-11-26
  • 2023-04-11
  • 2014-03-07
  • 2017-01-02
  • 2018-07-13
  • 1970-01-01
  • 1970-01-01
  • 2020-02-01
  • 1970-01-01
相关资源
最近更新 更多