为了账号安全,请及时绑定邮箱和手机立即绑定

使用 Python 遍历 JSON 对象

使用 Python 遍历 JSON 对象

宝慕林4294392 2021-09-14 16:14:58
我有一个 JSON 对象,想知道如何遍历该对象以提取“id”的值。{"totalSize": 5,"done": true,"records": [    {        "attributes": {            "type": "EventLogFile",            "url": "/services/data/v38.0/sobjects/EventLogFile/0AT1U000003kk7dWAA"        },        "Id": "0AT1U000003kk7dWAA"    },    {        "attributes": {            "type": "EventLogFile",            "url": "/services/data/v38.0/sobjects/EventLogFile/0AT1U000003kk7eWAA"        },        "Id": "0AT1U000003kk7eWAA" 我在下面尝试一些东西。sub_data = s["records"]["id"]for i in sub_data:        print(sub_data['id'])
查看完整描述

2 回答

?
哔哔one

TA贡献1854条经验 获得超8个赞

您可以将records键作为列表进行迭代,然后访问Id每个子字典的键:


for i in s["records"]:

    print(i['Id'])


查看完整回答
反对 回复 2021-09-14
?
慕工程0101907

TA贡献1887条经验 获得超5个赞

s = """{ "totalSize": 5, 

        "done": true, "records": [ 

            { "attributes": { 

                "type": "EventLogFile", 

                "url": "/services/data/v38.0/sobjects/EventLogFile/0AT1U000003kk7dWAA" }, 

                "Id": "0AT1U000003kk7dWAA" } 

        ] 

    }"""

s = json.loads(s)


[r['Id'] for r in s['records']]


['0AT1U000003kk7dWAA']


查看完整回答
反对 回复 2021-09-14
  • 2 回答
  • 0 关注
  • 400 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信