如何从JSON对象解析数据?

Led*_*ddo 4 ruby json

我刚刚开始涉足使用JSON Web服务,我在找到实际数据元素的最佳方法时遇到了一些麻烦.

我收到的响应已使用该JSON.parse方法转换为Ruby哈希.哈希看起来像这样:

{"response"=>{"code"=>2002, "payload"=>{"topic"=>[{"name"=>"Topic Name", "url"=>"http://www.something.com/topic", "hero_image"=>{"image_id"=>"05rfbwV0Nggp8", "hero_image_id"=>"0d600BZ7MZgLJ", "hero_image_url"=>"http://img.something.com/imageserve/0d600BZ7MZgLJ/60x60.jpg"}, "type"=>"PERSON", "search_score"=>10.0, "topic_id"=>"0eG10W4e3Aapo"}]}, "message"=>"Success"}}
Run Code Online (Sandbox Code Playgroud)

我想知道的是,获取"主题"数据的最简单方法是什么,所以我可以这样做:

topic.name = json_resp.name
topic.img = jsob_resp.hero_image_url
etc
Run Code Online (Sandbox Code Playgroud)

Dan*_*iro 7

你可以使用Hashie 's Mash.其中的最好的Twitter客户端的红宝石使用它,并将得到的界面非常干净,易于使用.我用不到60行包裹了美味rss api.

通常情况下,规范非常清楚地表明如何使用它.