[英]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.