Iterate over JSON Object using Python

前端 未结 2 1413
-上瘾入骨i
-上瘾入骨i 2021-01-26 02:10

I have a JSON object and was wondering how I can iterate over the object to pull values for \"id\".

{
\"totalSize\": 5,
\"done\": true,
\"records\": [
    {
             


        
相关标签:
2条回答
  • 2021-01-26 02:18
    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']
    
    0 讨论(0)
  • 2021-01-26 02:39

    You can iterate through the records key as a list and then access the Id key of each sub-dict:

    for i in s["records"]:
        print(i['Id'])
    
    0 讨论(0)
提交回复
热议问题