我正在使用Python(2.X)抓取并解析我从RiotGames LoL API获取的JSON数据,我遇到了一个奇怪的错误。
我正在加载json数据并通过attr读取数据attr,这很好,直到我达到某个attr,这显然是我试图从中提取它的对象,但是让Python抛出一个KeyError就可以了可以在下面的截图中看到。
这是发生错误的codesnippet。正如您所看到的,我打印对象(用于调试目的),然后解析所有attr,它工作正常,但在attr'doubleKills'中出于未知原因抛出KeyError。希望你们能帮忙^^
def parseJSON(self, jsonDump):
matchDetailDict = dict()
jsonobj = json.loads(jsonDump)
matchId = jsonobj['matchId']
tmpMatch = Match()
tmpMatch.matchID = matchId
tmpMatch.creationDate = jsonobj['matchCreation']
tmpMatch.matchDuration = jsonobj['matchDuration']
for participant, participantId in zip(jsonobj['participants'], jsonobj['participantIdentities']):
stats = participant['stats']
print stats
tmpStats = MatchPlayerStats()
tmpStats.playerID = participantId['player']['summonerId']
tmpStats.matchID = matchId
tmpStats.winner = stats['winner']
tmpStats.kills = stats['kills']
tmpStats.deaths = stats['deaths']
tmpStats.assists = stats['assists']
tmpStats.kda = (tmpStats.kills + tmpStats.assists)*1.0/max(tmpStats.deaths, 0.5)
tmpStats.visionWardsBoughtInGame = stats['visionWardsBoughtInGame']
tmpStats.sightWardsBoughtInGame = stats['sightWardsBoughtInGame']
tmpStats.championID = participant['championId']
tmpStats.doubleKills = participant['doubleKills'] #KeyError here!
tmpStats.firstBloodAssist = participant['firstBloodAssist']
tmpStats.firstBloodKill = participant['firstBloodKill']
tmpStats.killingSprees = participant['killingSprees']
tmpStats.wardsKilled = participant['wardsKilled']
tmpStats.wardsPlaced = participant['wardsPlaced']
tmpStats.unrealKills = participant['unrealKills']
matchDetailDict[tmpStats.playerID] = tmpStats
tmpMatch.playerStats = matchDetailDict
return tmpMatch
答案 0 :(得分:2)
您发布的终端上的JSON似乎来自stats
,但您尝试使用participant
上的密钥。
print 'doubleKills' in stats.keys()
应评估为True