在python中循环使用JSON
我是一个完全的初学者,我正在尝试为一门课程创建一个程序,它将通过一个关于一些结果的数据集。我很难进入以下领域的所有团队:在python中循环使用JSON,python,json,loops,Python,Json,Loops,我是一个完全的初学者,我正在尝试为一门课程创建一个程序,它将通过一个关于一些结果的数据集。我很难进入以下领域的所有团队: { "name": "English Premier League 2014/15", "rounds": [ { "name": "Matchday 1", "matches": [ { "date": "2014-08-16",
{
"name": "English Premier League 2014/15",
"rounds": [
{
"name": "Matchday 1",
"matches": [
{
"date": "2014-08-16",
"team1": {
"key": "manutd",
"name": "Manchester United",
"code": "MUN"
},
"team2": {
"key": "swansea",
"name": "Swansea",
"code": "SWA"
},
"score1": 1,
"score2": 2
},
{
"date": "2014-08-16",
"team1": {
"key": "leicester",
"name": "Leicester City",
"code": "LEI"
},
"team2": {
"key": "everton",
"name": "Everton",
"code": "EVE"
},
"score1": 2,
"score2": 2
}],
}],
}
我使用了以下代码行:
for matchday in js['rounds'] :
print(matchday['matches'][0]['team1']['name'])
这将打印每轮第一场比赛的第一队名称,但我想打印每轮所有第一队的名称。有人能给我一个提示吗?您应该添加第二个循环并迭代匹配:
for rounds in js['rounds']:
for matches in rounds['matches']:
print(matches['team1']['name'])
您应该添加第二个循环并迭代匹配:
for rounds in js['rounds']:
for matches in rounds['matches']:
print(matches['team1']['name'])
您正在获取匹配列表中的第一项,只需在列表中再次循环
for matchday in js['rounds'] :
match_day = matchday['matches']
for each_match in match_day:
print(each_match['team1']['name'])
您正在获取匹配列表中的第一项,只需在列表中再次循环
for matchday in js['rounds'] :
match_day = matchday['matches']
for each_match in match_day:
print(each_match['team1']['name'])
首先尝试一个较小的例子。另请参见:在每个
matchday[“matches”]
循环中,您需要一个内部。注意:为了提高可读性/一致性,您应该将matchday
重命名为round
。首先尝试使用较小的示例。另请参见:在每个matchday[“matches”]
循环中,您需要一个内部。注意:为了提高可读性/一致性,您应该将matchday
重命名为round
。