Python JSON 正在解析的对象中未丢失键的 KeyError
Python JSON KeyError for key that is not missing in object being parsed
我正在使用 Python (2.X) 抓取和解析从 RiotGames LoL API 获得的 JSON 数据,我 运行 变成一个奇怪的错误。
我正在加载 json 数据并通过 attr 读取数据 attr,这很好,直到我遇到某个 attr,该 attr 显然在我试图从中提取它的对象中,但使Python 引发 KeyError,如下面的屏幕截图所示。
这是发生错误的代码片段。如您所见,我打印对象(用于调试目的),然后解析所有 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
您发布的终端上的 JSON 似乎来自 stats
,但您正在尝试使用 participant
上的密钥。
print 'doubleKills' in stats.keys()
应评估为 True
我正在使用 Python (2.X) 抓取和解析从 RiotGames LoL API 获得的 JSON 数据,我 运行 变成一个奇怪的错误。
我正在加载 json 数据并通过 attr 读取数据 attr,这很好,直到我遇到某个 attr,该 attr 显然在我试图从中提取它的对象中,但使Python 引发 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
您发布的终端上的 JSON 似乎来自 stats
,但您正在尝试使用 participant
上的密钥。
print 'doubleKills' in stats.keys()
应评估为 True