【问题标题】:Can't format the JSON data无法格式化 JSON 数据
【发布时间】:2019-01-16 07:28:36
【问题描述】:

以下是我从 API 调用中获得的示例 JSON 数据。但是当我尝试格式化 JSON 数据时,我做不到。

{
    "jsonrpc": "2.0",
    "result": [
        {
            "status": "3",
            "name": "Windows",
            "triggers": [
                { "triggerid": "11234" },
                { "triggerid": "5465" },
                { "triggerid": "56465" },
                { "triggerid": "56465" },
                { "triggerid": "54364" },
                { "triggerid": "564654" },
                { "triggerid": "564365" },
                { "triggerid": "5434" },
                { "triggerid": "54354" },
                { "triggerid": "5454" },
                { "triggerid": "5645" },
                { "triggerid": "543654" },
                { "triggerid": "546543" }
            ],
            "items": [
                { "name": "connection check" },
                { "name": "Version of apache running" },
                { "name": "Average IOPS " },
                { "name": "Average wrtie speed" },
                { "name": "file in speed" }
            ],
            "templateid": "456434",
            "discoveries": []
        },
        {
            "status": "3",
            "name": "linux_server",
            "triggers": [
                { "triggerid": "11234" },
                { "triggerid": "5465" },
                { "triggerid": "56465" },
                { "triggerid": "56465" },
                { "triggerid": "54364" },
                { "triggerid": "564654" },
                { "triggerid": "564365" },
                { "triggerid": "5434" },
                { "triggerid": "54354" },
                { "triggerid": "5454" },
                { "triggerid": "5645" },
                { "triggerid": "543654" },
                { "triggerid": "123543" }
            ],
            "items": [
                { "name": "connection check" },
                { "name": "Version of docker running" },
                { "name": "Average IOPS " },
                { "name": "Average wrtie speed" },
                { "name": "file in speed" }
            ],
            "templateid": "456434",
            "discoveries": []
        }
    ]
}

我的代码:

output = requests.post(url, data=apache_data, headers=headers)
content_out = json.loads(output.content)
content_out = json.dumps(content_out)

for key in content_out .items():
  print(key)

错误:AttributeError: 'str' object has no attribute 'items'

我想要“name":"windows".......直到"discoveries": []}作为一个字典。就像那个"name": "linux_server"............直到"discoveries": []}作为另一个字典..任何帮助将不胜感激。

【问题讨论】:

  • 请格式化您的代码并定义template_out 变量。
  • 您在此处粘贴的 JSON 示例至少缺少一个尾随 ]}

标签: python json python-requests


【解决方案1】:

你在使用requests吗?您可以通过调用output.json()

以字典形式访问 JSON

所以我认为你的代码看起来像这样:

output = requests.post(url, data=apache_data, headers=headers)

for result in output.json()['result']:
    print(result['name'], result['status'], result['templateid'])

这是输出:

Windows 3 456434
linux_server 3 456434

在您的示例中,Windowslinux_serverresult 列表中的两个字典。

【讨论】:

    【解决方案2】:

    您留下了一行调试/实验代码,将您想要的已解析 JSON 转换回字符串。尝试删除它:

    output = requests.post(url, data=apache_data, headers=headers )
    # This parses your JSON correctly.
    content_out = json.loads(ouput.content)
    # This line turns your parsed JSON back into a string. You don't need this.
    # content_out = json.dumps(content_out)
    
    for key in content_out.items():
        print (key)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-18
      • 2020-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多