You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Python中解析嵌套JSON并提取所有waiverId

提取所有waiverId的正确方法

你的代码问题在于:直接遍历data['checkins']时,遍历的是这个外层字典的键名(也就是fromDts、toDts这些字段),而不是目标的签到数据列表。要拿到waiverId,需要先定位到内层的checkins列表。

方法1:普通循环提取

# 先获取内层的签到数据列表
checkins_list = data['checkins']['checkins']
waiver_ids = []
for item in checkins_list:
    # 从每个签到项中提取waiverId并添加到列表
    waiver_ids.append(item['waiverId'])
print(waiver_ids)

执行后会输出:['(id1)', '(id2)']

方法2:列表推导式(更简洁)

waiver_ids = [item['waiverId'] for item in data['checkins']['checkins']]
print(waiver_ids)

这种写法和上面的循环效果完全一致,只是代码更紧凑。

额外提示

如果担心某些签到项可能缺失waiverId字段,可以加个判断避免报错:

waiver_ids = [item['waiverId'] for item in data['checkins']['checkins'] if 'waiverId' in item]

内容的提问来源于stack exchange,提问作者mobil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 14:25:44