ruby hash to object - Parsing data from JSON object
Posted
by Leddo
on Stack Overflow
See other posts from Stack Overflow
or by Leddo
Published on 2010-05-27T20:38:54Z
Indexed on
2010/05/27
20:41 UTC
Read the original article
Hit count: 159
Hi all, I'm just starting to dabble in consuming a JSON webservice, and I am having a little trouble working out the best way to get to the actual data elements.
I am receiving a response which has been converted into a ruby hash using the JSON.parse method. The hash looks like this:
{"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"}}
What I would like to know, is what is the easiest way to get to the "topic" data so I can do something like:
topic.name = json_resp.name
topic.img = jsob_resp.hero_image_url
etc
Many thanks for any help you can offer.
Regards Chris
© Stack Overflow or respective owner