使用Ruby获取单个JSON对象

时间:2010-08-13 01:45:44

标签: ruby json httparty

如果这是一个非常愚蠢的问题,请原谅我,但如何通过HTTParty从JSON GET中获取单个项目?例如:

require 'rubygems'
require 'httparty'
require 'pp'

player = pp HTTParty.get('http://api.dribbble.com/players/1')

将返回此信息:

{"name"=>"Dan Cederholm",
 "avatar_url"=>
  "http://dribbble.com/system/users/1/avatars/thumb/dancederholm-peek.jpg?1261060245",
 "twitter_screen_name"=>"simplebits",
 "shots_count"=>148,
 "location"=>"Salem, MA",
 "created_at"=>"2009/07/07 21:51:22 -0400",
 "following_count"=>375,
 "draftees_count"=>103,
 "url"=>"http://dribbble.com/players/simplebits",
 "id"=>1,
 "followers_count"=>2119,
 "drafted_by_player_id"=>nil}

如何获得单个项目,例如名称?

2 个答案:

答案 0 :(得分:1)

player["name"]

返回的对象是Hash

答案 1 :(得分:0)

player.name似乎是最自然的尝试。通常,您可以检查player.class以确定结果类型。