4

I'm just starting to dabble in consuming a JSON web service, 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

1 Answer 1

7

You can use Hashie's Mash . One of the best twitter clients for ruby uses it, and the resulting interface is very clean and easy to use. I've wrapped over Delicious rss api with it in less than 60 lines.

As usuall, the specs show very clearly how to use it.

Sign up to request clarification or add additional context in comments.

1 Comment

Twitter client no longer uses the Hashie gem, however it's still an excellent choice for for the OP's requirement.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.