无法格式化JSON数据

时间:2018-08-08 22:53:18

标签: python json python-requests

以下是我从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": []}作为另一本词典。任何帮助。

2 个答案:

答案 0 :(得分: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列表中的两个字典。

答案 1 :(得分:0)

您已经离开了一行调试/实验代码,该代码将您要解析的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)