问题描述
我对Python和JSON很陌生。我正在构建一个工具,可以让我快速分析玩家列表。
我正在尝试解析以下JSON,并从所有条目中获取“百分号”键的平均和。
[
{
"encounterID": 663,"encounterName": "Lucifron","class": "Priest","spec": "Healer","rank": 1471,"outOf": 4463,"duration": 48171,"startTime": 1597001801681,"reportID": "74LJdpnCZahwmxyW","fightID": 9,"difficulty": 3,"characterID": 42962953,"characterName": "Insanepriest","server": "Lakeshire","percentile": 67.03123111833183,"ilvlKeyOrPatch": 70,"azeritePowers": [],"total": 225.509,"estimated": true
},{
"encounterID": 664,"encounterName": "Magmadar","rank": 3205,"outOf": 5055,"duration": 67599,"startTime": 1597001988440,"fightID": 11,"percentile": 36.597235868212486,"total": 130.623,{
"encounterID": 667,"encounterName": "Shazzrah","rank": 2655,"outOf": 12663,"duration": 27960,"startTime": 1597003660411,"fightID": 32,"percentile": 79.02778951070532,"total": 441.345,]
这是我正在使用的python代码,我只设法对所有项目求和。
response = requests.get('https://classic.warcraftlogs.com/v1/parses/character/Insanepriest/Lakeshire/EU?metric=dps&zone=1000&partition=2&api_key=6436362d91e87781841b4b031f7c0a6c')
response.raise_for_status()
# access JSOn content
jsonResponse = response.json()
parse = round(jsonResponse[0]["percentile"],1)
sum = 0
for parse in jsonResponse:
if parse and "percentile" in parse.keys():
sum += parse["percentile"]
print(sum)
任何帮助将不胜感激。很抱歉,如果我在这里问一个愚蠢的问题,但我在网上找不到任何东西。
最好的问候
解决方法
percentiles = [round(i['percentile'],1) for i in jsonResponse]
percentilesSum = sum(percentiles)
#I dont know what you mean by average sum,I count it as average
percentilesAvg = percentilesSum / len(percentiles)