使用python从JSON格式提取数据

时间:2016-04-22 09:27:39

标签: python json

这是我的JSON输出。

{
    "data": {
        "BaselineLastFinish": "N/A",
        "BlockSchedStart": "N/A",
        "BlockSchedStop": "N/A",
        "CurrentCycleDay0": "Tue, 17 Apr 2016 00:00:00 GMT",
        "CurrentCycleSchedMaint": "N/A",
        "LastReboot": "Tue, 06 Oct 2015 20:46:16 +0000",
        "LastRunFailCount": "0",
        "LastRunStatus": "No Results",
        "LastRunSuccessCount": "0",
        "MaintInProgress": "False",
        "NextCycleDay0": "Tue, 19 May 2016 00:00:00 GMT",
        "NextCycleSchedMaint": "N/A",
        "PPID": null,
        "PatchProfile": "",
        "PreviousCycleDay0": "Tue, 18 Mar 2016 00:00:00 GMT",
        "PreviousCycleSchedMaint": "N/A",
        "row": "CH1||N/A|N/A|N/A||0|0|False|Tue, 08 Oct 2015 20:46:16 +0000",
        "servername": "CH1"
    },
    "message": "OK",
    "status": "success",
    "status_code": "200",
    "timestamp": "2016-04-26 08:43:44",
    "uuid": "3533etetetet"
}

我试图从上面的JSON输出中提取LastReboot。我是以下面的方式尝试但没有运气。

response = requests.get(url)
if response.status_code == 200:
data = json.loads(response.text)
print data['LastReboot']

我收到以下错误::

Traceback (most recent call last)
File "atp.py", line 16, in <module>
    print data['LastReboot']
KeyError: 'LastReboot'

2 个答案:

答案 0 :(得分:3)

首先,您必须拨打&#39;数据&#39;,就像这里:

data['data']['LastReboot']

答案 1 :(得分:0)

您错过了data密钥,请尝试以下

>>>data['data']['LastReboot']
"Tue, 06 Oct 2015 20:46:16 +0000"