使用Python对JSON响应进行排序

时间:2015-12-02 01:26:31

标签: python json sorting

例如,需要帮助弄清楚如何按最高到最低的数字对JSON响应进行排序。以下是JSON响应的一部分:

{
"queue": "RANKED_SOLO_5x5",
"name": "Riven's Cutthroats",
"entries": [
  {
     "leaguePoints": 812,
     "isFreshBlood": false,
     "isHotStreak": false,
     "division": "I",
     "isInactive": false,
     "isVeteran": false,
     "losses": 277,
     "playerOrTeamName": "CLG Bunso",
     "playerOrTeamId": "19732914",
     "wins": 356
  },
  {
     "leaguePoints": 567,
     "isFreshBlood": false,
     "isHotStreak": false,
     "division": "I",
     "isInactive": false,
     "isVeteran": false,
     "losses": 56,
     "playerOrTeamName": "SKT Frost",
     "playerOrTeamId": "66401633",
     "wins": 160
  },
  {
     "leaguePoints": 751,
     "isFreshBlood": false,
     "isHotStreak": false,
     "division": "I",
     "isInactive": false,
     "isVeteran": true,
     "losses": 421,
     "playerOrTeamName": "C9 Hard",
     "playerOrTeamId": "47836799",
     "wins": 494
  },
  {
     "leaguePoints": 587,
     "isFreshBlood": false,
     "isHotStreak": true,
     "division": "I",
     "isInactive": false,
     "isVeteran": false,
     "losses": 157,
     "playerOrTeamName": "ShadowFiendv",
     "playerOrTeamId": "71181475",
     "wins": 265
  },
  {
     "leaguePoints": 1109,
     "isFreshBlood": false,
     "isHotStreak": false,
     "division": "I",
     "isInactive": false,
     "isVeteran": true,
     "losses": 353,
     "playerOrTeamName": "ApoIlo Price",
     "playerOrTeamId": "7250",
     "wins": 425
  },

现在,我已经抓住了我需要的必要信息,如下所示:

def getChallengerLadder(region, APIKey):
    URL = "https://" + region + ".api.pvp.net/api/lol/" + region +     "/v2.5/league/challenger?type=RANKED_SOLO_5x5&api_key=" + APIKey
    print (URL)
    response = requests.get(URL)
    return response.json()


   responseJSON3 = getChallengerLadder(region, APIKey)
    x = 0
    while True:
        print (responseJSON3['entries'][x]['leaguePoints'], responseJSON3['entries'][x]['playerOrTeamName'] )
         x += 1

结果我得到如下列表(再次,下面只是一个小样本):

608 Z Y Xydra
552 Silas Kroeger
1109 ApoIlo Price
601 Blem
587 Boy vs Girl
701 l am Bjerg
560 duo to homecomin

我想从最大到最小的数字对此列表进行排序,但我无法弄清楚如何做到这一点。任何帮助都会被贬低!有没有更好的方法,然后我已经完成了?我假设您必须将响应放入数组并对数组进行排序?或者有更好的方法吗?

1 个答案:

答案 0 :(得分:0)

leaguePoints

排序

(我只使用json模块创建完整的工作示例)

text = '''{
"queue": "RANKED_SOLO_5x5",
"name": "Riven's Cutthroats",
"entries": [
  {
     "leaguePoints": 812,
     "isFreshBlood": false,
     "isHotStreak": false,
     "division": "I",
     "isInactive": false,
     "isVeteran": false,
     "losses": 277,
     "playerOrTeamName": "CLG Bunso",
     "playerOrTeamId": "19732914",
     "wins": 356
  },
  {
     "leaguePoints": 567,
     "isFreshBlood": false,
     "isHotStreak": false,
     "division": "I",
     "isInactive": false,
     "isVeteran": false,
     "losses": 56,
     "playerOrTeamName": "SKT Frost",
     "playerOrTeamId": "66401633",
     "wins": 160
  },
  {
     "leaguePoints": 751,
     "isFreshBlood": false,
     "isHotStreak": false,
     "division": "I",
     "isInactive": false,
     "isVeteran": true,
     "losses": 421,
     "playerOrTeamName": "C9 Hard",
     "playerOrTeamId": "47836799",
     "wins": 494
  },
  {
     "leaguePoints": 587,
     "isFreshBlood": false,
     "isHotStreak": true,
     "division": "I",
     "isInactive": false,
     "isVeteran": false,
     "losses": 157,
     "playerOrTeamName": "ShadowFiendv",
     "playerOrTeamId": "71181475",
     "wins": 265
  },
  {
     "leaguePoints": 1109,
     "isFreshBlood": false,
     "isHotStreak": false,
     "division": "I",
     "isInactive": false,
     "isVeteran": true,
     "losses": 353,
     "playerOrTeamName": "ApoIlo Price",
     "playerOrTeamId": "7250",
     "wins": 425
  }
]
}'''

import json

# simulate `getChallengerLadder`
responseJSON3 = json.loads(text)

result = sorted(responseJSON3['entries'], key=lambda x:x['leaguePoints'])

print result