2016-09-05 37 views
-1

我的紅寶石非常生疏,我很難通過一些JSON來解析我從紅寶石中的HTTParty調用回來的東西。目前,我試圖獲取深入嵌套在解析的JSON響應中的'ladderId'屬性值。Ruby:如何找到匹配某些屬性的嵌套對象

response = HTTParty.get('myApiUrlHere') 
json = JSON.parse(response.body) 

ladder_id = ## // find within the "CurrentSeason" object a "ladder" 
      ## // object whose "league" property has the value 
      ## // "GRANDMASTER", and return that objects ladder ID 

如果我的措辭是混亂的,我應該期待值「197606」時,給出以下JSON回報

{ 
    "currentSeason": [{ 
     "ladder": [{ 
      "ladderName": "Alzadar Theta", 
      "ladderId": 197606, 
      "division": 5, 
      "rank": 65, 
      "league": "DIAMOND", 
      "matchMakingQueue": "LOTV_TWOS", 
      "wins": 8, 
      "losses": 2, 
      "showcase": true 
     }], 
     "characters": [{ 
      "id": 989277, 
      "realm": 1, 
      "displayName": "Warren", 
      "clanName": "PSISTORM Gaming", 
      "clanTag": "PSISTM", 
      "profilePath": "/profile/989277/1/Warren/" 
     }, { 
      "id": 5220949, 
      "realm": 1, 
      "displayName": "True", 
      "clanName": "PSISTORM Gaming", 
      "clanTag": "PSISTM", 
      "profilePath": "/profile/5220949/1/True/" 
     }], 
     "nonRanked": [] 
    }, { 
     "ladder": [{ 
      "ladderName": "Kerrigan Kilo", 
      "ladderId": 197160, 
      "division": 1, 
      "rank": 95, 
      "league": "MASTER", 
      "matchMakingQueue": "LOTV_TWOS_COMP", 
      "wins": 1, 
      "losses": 0, 
      "showcase": true 
     }], 
     "characters": [{ 
      "id": 3650249, 
      "realm": 1, 
      "displayName": "CranK", 
      "clanName": "Axiom eSports", 
      "clanTag": "Ax", 
      "profilePath": "/profile/3650249/1/CranK/" 
     }, { 
      "id": 5220949, 
      "realm": 1, 
      "displayName": "True", 
      "clanName": "PSISTORM Gaming", 
      "clanTag": "PSISTM", 
      "profilePath": "/profile/5220949/1/True/" 
     }], 
     "nonRanked": [] 
    }, { 
     "ladder": [], 
     "characters": [{ 
      "id": 2238820, 
      "realm": 1, 
      "displayName": "Sugar", 
      "clanName": "PSISTORM Gaming", 
      "clanTag": "PSISTM", 
      "profilePath": "/profile/2238820/1/Sugar/" 
     }, { 
      "id": 2481800, 
      "realm": 1, 
      "displayName": "starkiller", 
      "clanName": "Ørdo Equitum", 
      "clanTag": "ØE", 
      "profilePath": "/profile/2481800/1/starkiller/" 
     }, { 
      "id": 5220949, 
      "realm": 1, 
      "displayName": "True", 
      "clanName": "PSISTORM Gaming", 
      "clanTag": "PSISTM", 
      "profilePath": "/profile/5220949/1/True/" 
     }], 
     "nonRanked": [{ 
      "mmq": "LOTV_THREES", 
      "gamesPlayed": 2 
     }] 
    }, { 
     "ladder": [{ 
      "ladderName": "Carrier Zeta", 
      "ladderId": 197921, 
      "division": 1, 
      "rank": 1, 
      "league": "GRANDMASTER", 
      "matchMakingQueue": "LOTV_SOLO", 
      "wins": 187, 
      "losses": 51, 
      "showcase": true 
     }], 
     "characters": [{ 
      "id": 5220949, 
      "realm": 1, 
      "displayName": "True", 
      "clanName": "PSISTORM Gaming", 
      "clanTag": "PSISTM", 
      "profilePath": "/profile/5220949/1/True/" 
     }], 
     "nonRanked": [] 
    }], 
    "previousSeason": [{ 
     "ladder": [{ 
      "ladderName": "Marine Foxtrot", 
      "ladderId": 196150, 
      "division": 1, 
      "rank": 35, 
      "league": "MASTER", 
      "matchMakingQueue": "LOTV_TWOS_COMP", 
      "wins": 4, 
      "losses": 5, 
      "showcase": false 
     }, { 
      "ladderName": "War Pigs Foxtrot", 
      "ladderId": 196853, 
      "division": 11, 
      "rank": 73, 
      "league": "DIAMOND", 
      "matchMakingQueue": "LOTV_TWOS", 
      "wins": 2, 
      "losses": 0, 
      "showcase": false 
     }], 
     "characters": [{ 
      "id": 3650249, 
      "realm": 1, 
      "displayName": "CranK", 
      "clanName": "Axiom eSports", 
      "clanTag": "Ax", 
      "profilePath": "/profile/3650249/1/CranK/" 
     }, { 
      "id": 5220949, 
      "realm": 1, 
      "displayName": "True", 
      "clanName": "PSISTORM Gaming", 
      "clanTag": "PSISTM", 
      "profilePath": "/profile/5220949/1/True/" 
     }], 
     "nonRanked": [] 
    }, { 
     "ladder": [{ 
      "ladderName": "Mutalisk Echo", 
      "ladderId": 196647, 
      "division": 1, 
      "rank": 3, 
      "league": "GRANDMASTER", 
      "matchMakingQueue": "LOTV_SOLO", 
      "wins": 134, 
      "losses": 38, 
      "showcase": false 
     }], 
     "characters": [{ 
      "id": 5220949, 
      "realm": 1, 
      "displayName": "True", 
      "clanName": "PSISTORM Gaming", 
      "clanTag": "PSISTM", 
      "profilePath": "/profile/5220949/1/True/" 
     }], 
     "nonRanked": [] 
    }, { 
     "ladder": [], 
     "characters": [{ 
      "id": 2238820, 
      "realm": 1, 
      "displayName": "Sugar", 
      "clanName": "PSISTORM Gaming", 
      "clanTag": "PSISTM", 
      "profilePath": "/profile/2238820/1/Sugar/" 
     }, { 
      "id": 5220949, 
      "realm": 1, 
      "displayName": "True", 
      "clanName": "PSISTORM Gaming", 
      "clanTag": "PSISTM", 
      "profilePath": "/profile/5220949/1/True/" 
     }], 
     "nonRanked": [{ 
      "mmq": "LOTV_TWOS_COMP", 
      "gamesPlayed": 2 
     }] 
    }] 
} 
+1

我們期望看到您嘗試解決問題的更好例子。目前您的代碼不會嘗試從解析的數據中檢索任何內容。請閱讀「[問]」,包括鏈接頁面,「[mcve]」,包括證明您嘗試解決問題的最低代碼。另外,請儘量減少JSON示例,以證明問題。除此之外的任何事都浪費了我們的時間。 「[Stack Overflow用戶需要多少研究工作?](http://meta.stackoverflow.com/a/261593/128421)」也是有用的閱讀。 –

回答

3

它相當奇怪的JSON,下面的代碼應您的要求的工作:

obj = JSON.parse('your json string') 
desired_obj = obj["currentSeason"].select do |i| 
    !i["ladder"][0].nil? && 
    i["ladder"][0]['league'] == 'GRANDMASTER' 
end 

ladder_id = desired_obj[0]['ladder'][0]['ladderId'] 
#=> 197921 
+0

感謝魔杖製作者,這工作完美! 我同意它的漂亮奇怪的JSON ...使用外部API總是帶給我新的東西。我感謝您的幫助。 –