2010-08-31 59 views

回答

11

使用Mash类的hashie宝石。

tweet = Hashie::Mash.new(
    HTTParty.get("http://api.twitter.com/1/statuses/public_timeline.json").first 
) 
tweet.user.screen_name 
3

我写this helper class几天前:

class ObjectifiedHash 

    def initialize hash 
     @data = hash.inject({}) do |data, (key,value)| 
      value = ObjectifiedHash.new value if value.kind_of? Hash 
      data[key.to_s] = value 
      data 
     end 
    end 

    def method_missing key 
     if @data.key? key.to_s 
      @data[key.to_s] 
     else 
      nil 
     end 
    end 

end 

用例:

ojson = ObjectifiedHash.new(HTTParty.get('http://api.dribbble.com/players/simplebits')) 
ojson.shots_counts # => 150 
相关问题