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

问题描述

在我努力尝试的过程中,希望在这里找到一些建议。正在使用python 3.8.5。

我正在查询一个停车场预订系统,该系统返回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,"hours": 15,"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,"days": 0
                }

我已经尝试了很多dictionary.get和dictionary.items变体,但是似乎什么也做不到。

我尝试过类似的事情

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

这只会返回“ 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

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...