我有以下json输出:
{
"status" : "success",
"data" : {
"class" : "4A",
"students" : [
{
"name" : "Peter",
"lastname" : "Henderson",
"grades" : [
{
"subject" : "math",
"score" : "A"
},
{
"subject" : "english",
"score" : "B"
}
]
},
{
"name" : "Nick",
"lastname" : "Simons",
"grades" : [
{
"subject" : "math",
"score" : "B"
},
{
"subject" : "english",
"score" : "C"
}
]
}
]
}
}
我不知道如何为每个学生打印所有成绩。
我假设您需要一个for循环来遍历所有学生,然后再第二个for循环来检查所有学生的成绩,但是我在python中找不到此语法。
希望有人知道如何解决此问题,感谢您的宝贵时间!
答案 0 :(得分:1)
如果使用以下代码,则response
是您的json
:
data = response['data']['students']
for student in data:
print('{} {}:'.format(student['name'], student['lastname']))
for grade in student['grades']:
print('\t{} - {}'.format(grade['subject'], grade['score']))
这是您得到的:
Peter Henderson:
math - A
english - B
Nick Simons:
math - B
english - C