我希望能夠自動將JSON對象解析爲實例變量。例如,用這個JSON。自動將JSON對象映射到Ruby中的實例變量
require 'httparty'
json = HTTParty.get('http://api.dribbble.com/players/simplebits') #=> {"shots_count":150,"twitter_screen_name":"simplebits","avatar_url":"http://dribbble.com/system/users/1/avatars/thumb/dancederholm-peek.jpg?1261060245","name":"Dan Cederholm","created_at":"2009/07/07 21:51:22 -0400","location":"Salem, MA","following_count":391,"url":"http://dribbble.com/players/simplebits","draftees_count":104,"id":1,"drafted_by_player_id":null,"followers_count":2214}
我希望能夠做到這一點:
json.shots_count
而且有它的輸出:
150
我怎麼可能做到這一點?
這看起來像是一個'OpenStruct'的遞歸實現。這個想法還有其他的實現,包括[recursive-open-struct gem](https://rubygems.org/gems/recursive-open-struct)。 – 2012-04-16 20:51:00