Python 3 - 从嵌套字典中的键中提取值

Python 3 - Extracting value from key in nested dictionary

希望在这里得到一些指导,因为我正在努力尝试。正在使用 python 3.8.5.

我正在查询 returns 一个 json 可用性列表的停车场预订系统。结果有很多嵌套字典,我正在努力提取我想要的值。

这就是我一直在做的事情:

import requests
import json

enquiry = requests.post(url.....) #queries api, this works fine

results = enquiry.text #extracts response data

dictionary = json.loads(results) #convert response to python dict

如果有一个空位,我会得到这个输出(抱歉长度)。如果有多个插槽可用,我会重复相同的输出:

{
    "data": {
        "services": [{
            "id": null,
            "name": null,
            "services": [{
                "id": null,
                "name": "Car Park",
                "met": true,
                "filterCount": 1,
                "primary": true,
                "options": [{
                    "id": "9",
                    "images": [],
                    "available": true,
                    "calendarId": "AAAA",
                    "templateId": "BBBB",
                    "capacity": 0,
                    "name": "Car Park Slot 3",
                    "sessionId": "CCCC",
                    "functions": null,
                    "startDate": "2020-08-18T13:30:00Z", <--this is what I want to extract
                    "endDate": "2020-08-18T14:30:00Z",
                    "geo": {
                        "lat": 0.0,
                        "lng": 0.0
                    },
                    "selected": false,
                    "linkedServices": [],
                    "tiers": null
                }]
            }],
            "currentBookingId": null,
            "startDate": {
                "ms": 1597757400000,
                "year": 2020,
                "month": 8,
                "day": 18,
                "dayOfWeek": 2,
                "time": {
                    "seconds": 0,
                    "minutes": 30,
                    "hours": 14,
                    "days": 0
                }
            },
            "endDate": {
                "ms": 1597761000000,
                "year": 2020,
                "month": 8,
                "day": 18,
                "dayOfWeek": 2,
                "time": {
                    "seconds": 0,
                    "minutes": 30,
                    "hours": 15,
                    "days": 0
                }
            },
            "sessionId": "2222222",
            "chargeType": 1,
            "hasPrimaryBookable": false,
            "hasBookable": false,
            "hasDiscounts": false,
            "hasMultipleTiers": false,
            "isPreferred": false,
            "primaryServiceAvailable": true,
            "primaryServiceId": null,
            "primaryServiceType": "undefined",
            "unavailableAttendees": []
        }],
        "bookingLimit": null
    },
    "success": true,
    "suppress": false,
    "version": "2.3.293",
    "message": null,
    "result": null,
    "errors": null,
    "code": null,
    "flags": 0,
    "redirect": null
}

我要提取:

"startDate": "2020-08-18T13:30:00Z"

来自任何返回槽中的每个键值对。但是,我无法解决。

提取整个嵌套字典也将包含相同的数据,但之后需要做更多的工作来整理它。

"startDate": {
                "ms": 1597757400000,
                "year": 2020,
                "month": 8,
                "day": 18,
                "dayOfWeek": 2,
                "time": {
                    "seconds": 0,
                    "minutes": 30,
                    "hours": 14,
                    "days": 0
                }

我尝试了很多 dictionary.get 和 dictionary.items 变体,但似乎无济于事。

我试过

key = ('startDate')
availability = dictionary.get(key)
print(availability)

这只是 returns 'none',所以认为我离题了

有什么指点吗?

提前致谢!

感谢您提供完整数据。它使测试更容易 :)

我不得不替换 null -> None,true -> True,false -> False

slot = {
    "data": {
        "services": [{
            "id": None,
            "name": None,
            "services": [{
                "id": None,
                "name": "Car Park",
                "met": True,
                "filterCount": 1,
                "primary": True,
                "options": [{
                    "id": "9",
                    "images": [],
                    "available": True,
                    "calendarId": "AAAA",
                    "templateId": "BBBB",
                    "capacity": 0,
                    "name": "Car Park Slot 3",
                    "sessionId": "CCCC",
                    "functions": None,
                    "startDate": "2020-08-18T13:30:00Z", # <--this is what I want to extract
    ................
    
    
print("Start Date:", slot['data']['services'][0]['services'][0]['options'][0]['startDate'])

输出

Start Date: 2020-08-18T13:30:00Z