繁体   English   中英

使用Python遍历JSON对象

[英]Iterate over JSON Object using Python

我有一个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'])

您可以遍历records键作为列表,然后访问每个子字典的Id键:

for i in s["records"]:
    print(i['Id'])
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']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM