所以我有这个python代码:
import requests
import json
url = requests.get('https://ow-api.com/v1/stats/pc/eu/rGInfini-2810/profile')
data = [url.json()]
with open('sr.json', 'w') as output:
json.dump(data, output, indent=4)
with open('sr.json', 'r') as file:
data = json.load(file)
for rank in data['ratings']:
print('Role: ' + rank['role'])
print('SR: ' + rank['level'])
但是我无法获取它来输出json文件中的排名。我将如何去做?
JSON数据如下:
{
"competitiveStats": {
"awards": {
"cards": 0,
"medals": 28,
"medalsBronze": 6,
"medalsSilver": 17,
"medalsGold": 5
},
"games": {
"played": 14,
"won": 4
}
},
"endorsement": 3,
"endorsementIcon": "https://static.playoverwatch.com/svg/icons/endorsement-frames-3c9292c49d.svg#_3",
"gamesWon": 856,
"icon": "https://d15f34w2p8l1cc.cloudfront.net/overwatch/f5dcd83834fc807c6d1452479828e57bf5a0c630a57e1f792854efc01c0610e6.png",
"level": 11,
"levelIcon": "https://d15f34w2p8l1cc.cloudfront.net/overwatch/69c2c1aff0db8429a980bad7db76a3388003e43f0034097dc4cfa7f13c5de7d7.png",
"name": "rGInfini#2810",
"prestige": 3,
"prestigeIcon": "https://d15f34w2p8l1cc.cloudfront.net/overwatch/4a2c852a16043f613b7bfac33c8536dd9f9621a3d567174cb4ad9a80e3b13102.png",
"private": false,
"quickPlayStats": {
"awards": {
"cards": 64,
"medals": 833,
"medalsBronze": 258,
"medalsSilver": 310,
"medalsGold": 265
},
"games": {
"played": 0,
"won": 134
}
},
"rating": 2322,
"ratingIcon": "https://d1u1mce87gyfbn.cloudfront.net/game/rank-icons/rank-GoldTier.png",
"ratings": [
{
"level": 2329,
"role": "damage",
"roleIcon": "https://static.playoverwatch.com/img/pages/career/icon-offense-6267addd52.png",
"rankIcon": "https://d1u1mce87gyfbn.cloudfront.net/game/rank-icons/rank-GoldTier.png"
},
{
"level": 2316,
"role": "support",
"roleIcon": "https://static.playoverwatch.com/img/pages/career/icon-support-46311a4210.png",
"rankIcon": "https://d1u1mce87gyfbn.cloudfront.net/game/rank-icons/rank-GoldTier.png"
}
]
}
将json-data放入列表的目的是什么?忽略它,一切都会按预期进行。
import requests
import json
URL = 'https://ow-api.com/v1/stats/pc/eu/rGInfini-2810/profile'
response = requests.get(URL)
data = response.json()
with open('sr.json', 'w') as output:
json.dump(data, output, indent=4)
with open('sr.json', 'r') as file:
data = json.load(file)
for rank in data['ratings']:
print(f"Role: {rank['role']}")
print(f"SR: {rank['level']}")
输出为:
Role: damage
SR: 2329
Role: support
SR: 2316