使用Ruby从JSON响应中检索id

时间:2013-11-30 02:20:27

标签: ruby json

我正在使用Ruby 2.0构建一个应用程序,在将字符串解析为JSON之后,我有兴趣检索下面列出的所有game_id字段。解析完成之后,Ruby中是否有一种简单的方法来通过解析的JSON响应进行映射/收集?这是我正在使用的JSON的一个例子。

{
    "season_type"=>"Regular",
    "sport"=>"NBA",
    "gamedate"=>"11/29/2013",
    "updated_at"=>"2013-11-29 23:35:17 UTC",
    "games"=>[
        {
            "game"=>{
                "home_team_name"=>"Charlotte",
                "game_id"=>19089,
                "visiting_team_name"=>"Milwaukee",
                "home_team_id"=>152,
                "gametime"=>"07:00 PM",
                "visiting_team_id"=>103
            }
        },
        {
            "game"=>{
                "home_team_name"=>"Orlando",
                "game_id"=>19091,
                "visiting_team_name"=>"San Antonio",
                "home_team_id"=>117,
                "gametime"=>"07:00 PM",
                "visiting_team_id"=>110
            }
        },
        {
            "game"=>{
                "home_team_name"=>"Toronto",
                "game_id"=>19092,
                "visiting_team_name"=>"Miami",
                "home_team_id"=>118,
                "gametime"=>"07:00 PM",
                "visiting_team_id"=>115
            }
        },
        {
            "game"=>{
                "home_team_name"=>"Atlanta",
                "game_id"=>19096,
                "visiting_team_name"=>"Dallas",
                "home_team_id"=>91,
                "gametime"=>"07:30 PM",
                "visiting_team_id"=>95
            }
        },
        {
            "game"=>{
                "home_team_name"=>"Boston",
                "game_id"=>19101,
                "visiting_team_name"=>"Cleveland",
                "home_team_id"=>92,
                "gametime"=>"07:30 PM",
                "visiting_team_id"=>94
            }
        },
        {
            "game"=>{
                "home_team_name"=>"Detroit",
                "game_id"=>19104,
                "visiting_team_name"=>"LA Lakers",
                "home_team_id"=>97,
                "gametime"=>"07:30 PM",
                "visiting_team_id"=>102
            }
        },
        {
            "game"=>{
                "home_team_name"=>"Houston",
                "game_id"=>19106,
                "visiting_team_name"=>"Brooklyn",
                "home_team_id"=>99,
                "gametime"=>"08:00 PM",
                "visiting_team_id"=>104
            }
        },
        {
            "game"=>{
                "home_team_name"=>"Indiana",
                "game_id"=>19110,
                "visiting_team_name"=>"Washington",
                "home_team_id"=>100,
                "gametime"=>"08:00 PM",
                "visiting_team_id"=>113
            }
        },
        {
            "game"=>{
                "home_team_name"=>"Oklahoma City",
                "game_id"=>19108,
                "visiting_team_name"=>"Golden State",
                "home_team_id"=>111,
                "gametime"=>"08:00 PM",
                "visiting_team_id"=>98
            }
        },
        {
            "game"=>{
                "home_team_name"=>"Philadelphia",
                "game_id"=>19112,
                "visiting_team_name"=>"New Orleans",
                "home_team_id"=>106,
                "gametime"=>"08:00 PM",
                "visiting_team_id"=>114
            }
        },
        {
            "game"=>{
                "home_team_name"=>"Denver",
                "game_id"=>19116,
                "visiting_team_name"=>"New York",
                "home_team_id"=>96,
                "gametime"=>"09:00 PM",
                "visiting_team_id"=>105
            }
        },
        {
            "game"=>{
                "home_team_name"=>"Utah",
                "game_id"=>19119,
                "visiting_team_name"=>"Phoenix",
                "home_team_id"=>112,
                "gametime"=>"09:00 PM",
                "visiting_team_id"=>107
            }
        },
        {
            "game"=>{
                "home_team_name"=>"Sacramento",
                "game_id"=>19123,
                "visiting_team_name"=>"LA Clippers",
                "home_team_id"=>109,
                "gametime"=>"10:00 PM",
                "visiting_team_id"=>101
            }
        }
    ]
}

1 个答案:

答案 0 :(得分:1)

简单:

parsed_json['games'].map { |g| g['game']['game_id'] }