使用Python迭代JSON

时间:2017-07-08 09:48:22

标签: python json python-3.x python-requests

我有以下JSON对象,我想迭代它:

 {
    "odata.metadata": "https://aaa/_api/$metadata#TimesheetActualData",
    "value": [{
        "PROJ_UID": "39bf6bca-8939-4df6-aa81-8a6c3b23c979",
        "TASK_UID": "48dae88e-59ec-e611-80d4-00155d002a1c",
        "ASSN_UID": "4ac8a599-59ec-e611-80d4-00155d002a1c",
        "RES_UID": "e4a6a30d-5da2-e611-80cd-00155d00b80f",
        "PERIOD_UID": "9a08cd04-7f1c-41f1-9a42-cf9b8b3479ea",
        "TS_UID": null,
        "PROJ_NAME": "001 - Module Training Project 1",
        "TASK_NAME": "Approve new or changed Services",
        "TASK_TREE": "> Sample AMS project schedule > Service Strategy > Service Portfolio Management > Approve new or changed Services",
        "TASK_TYPE": "Billable",
        "TASK_CLIENTID": 9,
        "START": "2017-03-05T00:00:00",
        "END": "2017-03-05T00:00:00",
        "PLANNED_WORK": "0.00",
        "PPM_ACTUAL": "0.00",
        "TS_ACTUAL": null,
        "COMMENT": null,
        "SYNC_FLAG": 0,
        "APPROVAL_FLAG": null,
        "CREATED_DATE": "2017-03-17T12:04:38.53",
        "MOD_DATE": "2017-06-09T01:01:42.177",
        "ToBeDeleted": 0,
        "Incident_No": null,
        "Sync_Mod_Date": "2017-06-08T19:31:40",
        "TASK_START_DATE": "2017-02-14T08:00:00",
        "TASK_FINISH_DATE": "2017-03-10T17:00:00",
        "NON_ACTUAL_TASK_FLAG": 0,
        "InvoiceMasterId": 0,
        "Invoice_Status": "0",
        "Billing_Hours": "0.00",
        "Billing_Hours_Flag": null,
        "Final_Amount": "0.00",
        "ProjectCode": null,
        "WORKDAY": 0
    }, {
        "PROJ_UID": "39bf6bca-8939-4df6-aa81-8a6c3b23c979",
        "TASK_UID": "48dae88e-59ec-e611-80d4-00155d002a1c",
        "ASSN_UID": "4ac8a599-59ec-e611-80d4-00155d002a1c",
        "RES_UID": "e4a6a30d-5da2-e611-80cd-00155d00b80f",
        "PERIOD_UID": "9a08cd04-7f1c-41f1-9a42-cf9b8b3479ea",
        "TS_UID": null,
        "PROJ_NAME": "001 - Module Training Project 1",
        "TASK_NAME": "Approve new or changed Services",
        "TASK_TREE": "> Sample AMS project schedule > Service Strategy > Service Portfolio Management > Approve new or changed Services",
        "TASK_TYPE": "Billable",
        "TASK_CLIENTID": 9,
        "START": "2017-03-06T00:00:00",
        "END": "2017-03-06T00:00:00",
        "PLANNED_WORK": "0.00",
        "PPM_ACTUAL": "0.00",
        "TS_ACTUAL": null,
        "COMMENT": null,
        "SYNC_FLAG": 0,
        "APPROVAL_FLAG": null,
        "CREATED_DATE": "2017-03-17T12:04:38.7",
        "MOD_DATE": "2017-06-09T01:01:42.177",
        "ToBeDeleted": 0,
        "Incident_No": null,
        "Sync_Mod_Date": "2017-06-08T19:31:40",
        "TASK_START_DATE": "2017-02-14T08:00:00",
        "TASK_FINISH_DATE": "2017-03-10T17:00:00",
        "NON_ACTUAL_TASK_FLAG": 0,
        "InvoiceMasterId": 0,
        "Invoice_Status": "0",
        "Billing_Hours": "0.00",
        "Billing_Hours_Flag": null,
        "Final_Amount": "0.00",
        "ProjectCode": null,
        "WORKDAY": 0
    }],
    "odata.nextLink": "https://aaaa/_api/TimesheetActualData?$skip=1000"
 }

但不知何故,代码无效:

header={'content-type': 'application/json'}
response=requests.get(url,headers=header)
jjj=json.dumps(str(response.content))
d=json.loads(jjj)
print(d['values'])

2 个答案:

答案 0 :(得分:1)

您不必明确地将response.content传递给json模块来解析它,因为requests具有执行此操作的功能:

d = response.json()

然后你可以迭代它的值:

for value in d['value']:
    ...

答案 1 :(得分:0)

变化

jjj=json.dumps(str(response.content))
d=json.loads(jjj)

d = json.loads(response.text)

What is the difference between 'content' and 'text'

  

r.text是unicode中响应的内容,而r.content是   以字节为单位的响应内容