我只是想遍历json数据并打印一个人的名字。我曾尝试查找有关人们如何执行此操作的解决方案,但我尝试过的所有方法均无效。
Python代码:
my_url = 'www.domain.com'
my_data = json.loads(requests.get(my_url).text)
# I know in for loop I can go into people and go into info. But i'd like how to know how to do it by just going into people
for person in my_data['people']:
print(person['info']['name'])
Json
{
"amt_people": 3,
"people": {
"1": {
"info": {
"name": "Mike"
}
},
"2": {
"info": {
"name": "Paul"
}
},
"3": {
"info": {
"name": "Sarah"
}
}
}
}
答案 0 :(得分:0)
people对象不是列表,而是字典,您无法像这样对字典进行迭代。这应该起作用。
my_url = 'www.domain.com'
my_data = json.loads(requests.get(my_url).text)
for id , person in my_data['people'].items():
print(person['info']['name'])