如何将JSON格式字符串转换为目标列表?Python技术问询
问题:将JSON字符串转换为指定结构的列表
你的测试代码无法得到预期结果,核心问题在于:
list(accept_suggested_timings)会把JSON字符串拆分成单个字符的列表,完全偏离需求list(a)仅提取了解析后字典的键,生成的是键的列表,也不是目标结构
解决方案
只需将解析得到的字典直接包裹在一个列表中即可,修改后的代码如下:
import json accept_suggested_timings = """{"2023-11-20 00:00:00":[{"startDateTime":"2023-11-20 14:00:00","endDateTime":"2023-11-20 16:00:00"},{"startDateTime":"2023-11-20 16:00:00","endDateTime":"2023-11-20 18:00:00"},{"startDateTime":"2023-11-20 18:00:00","endDateTime":"2023-11-20 20:00:00"},{"startDateTime":"2023-11-20 20:00:00","endDateTime":"2023-11-20 22:00:00"}],"2023-11-21 00:00:00":[{"startDateTime":"2023-11-21 14:00:00","endDateTime":"2023-11-21 16:00:00"},{"startDateTime":"2023-11-21 16:00:00","endDateTime":"2023-11-21 18:00:00"},{"startDateTime":"2023-11-21 18:00:00","endDateTime":"2023-11-21 20:00:00"},{"startDateTime":"2023-11-21 20:00:00","endDateTime":"2023-11-21 22:00:00"}],"2023-11-22 00:00:00":[{"startDateTime":"2023-11-22 14:00:00","endDateTime":"2023-11-22 16:00:00"},{"startDateTime":"2023-11-22 16:00:00","endDateTime":"2023-11-22 18:00:00"},{"startDateTime":"2023-11-22 18:00:00","endDateTime":"2023-11-22 20:00:00"},{"startDateTime":"2023-11-22 20:00:00","endDateTime":"2023-11-22 22:00:00"}],"2023-11-23 00:00:00":[{"startDateTime":"2023-11-23 14:00:00","endDateTime":"2023-11-23 16:00:00"},{"startDateTime":"2023-11-23 16:00:00","endDateTime":"2023-11-23 18:00:00"},{"startDateTime":"2023-11-23 18:00:00","endDateTime":"2023-11-23 20:00:00"},{"startDateTime":"2023-11-23 20:00:00","endDateTime":"2023-11-23 22:00:00"}],"2023-11-24 00:00:00":[{"startDateTime":"2023-11-24 14:00:00","endDateTime":"2023-11-24 16:00:00"},{"startDateTime":"2023-11-24 16:00:00","endDateTime":"2023-11-24 18:00:00"},{"startDateTime":"2023-11-24 18:00:00","endDateTime":"2023-11-24 20:00:00"},{"startDateTime":"2023-11-24 20:00:00","endDateTime":"2023-11-24 22:00:00"}],"2023-11-27 00:00:00":[{"startDateTime":"2023-11-27 14:00:00","endDateTime":"2023-11-27 16:00:00"},{"startDateTime":"2023-11-27 16:00:00","endDateTime":"2023-11-27 18:00:00"},{"startDateTime":"2023-11-27 18:00:00","endDateTime":"2023-11-27 20:00:00"},{"startDateTime":"2023-11-27 20:00:00","endDateTime":"2023-11-27 22:00:00"}],"2023-11-28 00:00:00":[{"startDateTime":"2023-11-28 14:00:00","endDateTime":"2023-11-28 16:00:00"},{"startDateTime":"2023-11-28 16:00:00","endDateTime":"2023-11-28 18:00:00"},{"startDateTime":"2023-11-28 18:00:00","endDateTime":"2023-11-28 20:00:00"},{"startDateTime":"2023-11-28 20:00:00","endDateTime":"2023-11-28 22:00:00"}],"2023-11-29 00:00:00":[{"startDateTime":"2023-11-29 14:00:00","endDateTime":"2023-11-29 16:00:00"},{"startDateTime":"2023-11-29 16:00:00","endDateTime":"2023-11-29 18:00:00"},{"startDateTime":"2023-11-29 18:00:00","endDateTime":"2023-11-29 20:00:00"},{"startDateTime":"2023-11-29 20:00:00","endDateTime":"2023-11-29 22:00:00"}],"2023-11-30 00:00:00":[{"startDateTime":"2023-11-30 14:00:00","endDateTime":"2023-11-30 16:00:00"},{"startDateTime":"2023-11-30 16:00:00","endDateTime":"2023-11-30 18:00:00"},{"startDateTime":"2023-11-30 18:00:00","endDateTime":"2023-11-30 20:00:00"},{"startDateTime":"2023-11-30 20:00:00","endDateTime":"2023-11-30 22:00:00"}]}""" # 解析JSON字符串为字典 parsed_dict = json.loads(accept_suggested_timings) # 将字典包裹进列表,得到预期结构 result = [parsed_dict] # 打印结果 print(result)
关键说明
json.loads(accept_suggested_timings)将JSON字符串正确解析为Python字典,这一步逻辑无误- 直接用
[parsed_dict]把整个字典封装进列表,即可生成外层是列表、内部保留原字典完整结构的目标结果
内容的提问来源于stack exchange,提问作者Ben David
相关产品推荐
相关产品推荐

