Extraxcting Json objects from within JSON string (Ruby/ Rails)?

You should be able to decode anything in JSON format using the standard JSON library: JSON. Load(...) It will throw exceptions on malformed JSON data, so be sure to test it thoroughly and make sure it can handle all the important cases If you're trying to navigate the structure of the JSON itself, you probably need to write a series of recursive methods that handle each case along the way. A good pattern to start with is this: data.

Each do |key, value| case (key) when 'someKey' handle_some_key(value) when 'otherKey' handle_other_key(value) end end You can either break out the behavior into methods as in this example, or inline it if the logic is fairly straightforward As a note, an alternative to Array. New is simply as it is in JavaScript. For example: pages = You'll see this used frequently in most Ruby examples.

The alternative to Hash. New is { }.

You should be able to decode anything in JSON format using the standard JSON library: JSON. Load(...) It will throw exceptions on malformed JSON data, so be sure to test it thoroughly and make sure it can handle all the important cases. If you're trying to navigate the structure of the JSON itself, you probably need to write a series of recursive methods that handle each case along the way.

A good pattern to start with is this: @data. Each do |key, value| case (key) when 'someKey' handle_some_key(value) when 'otherKey' handle_other_key(value) end end You can either break out the behavior into methods as in this example, or inline it if the logic is fairly straightforward. As a note, an alternative to Array.

New is simply as it is in JavaScript. For example: @pages = You'll see this used frequently in most Ruby examples. The alternative to Hash.

New is { }.

The key part is the bit im having trouble with. I don't know how to compare a specific part of the json with a plain string. Javascript short would by document.getElementById() or get by xpath.Is there any way to compare it?

P. S thanks for the note on the arrays – OVERTONE Aug 10 at 15:29 @OVERTONE: "thanks" is generally traduced with +1 on stackoverflow :) – apneadiving Aug 10 at 15:44 Ok so theres just 2 parts to this I don't understand. @data would have to be an array or hash for it to use the .

Each method. But I have no idea how to deserialize the json into one of these. Second is kind of dependent on 1.

If theres no way to get data then theres no way to compare the keys. So short recap, how do I turn the JSON into an array or hash that I can cycle through? – OVERTONE Aug 10 at 15:51 @apeneadiving noted – OVERTONE Aug 10 at 15:56 Maybe I'm missing something, but the JSON example you provided has both hashes and arrays in it.

There's also something missing, probably because it was truncated. – tadman Aug 10 at 16:17.

The following works: json = {"respPages"=>{"page"=>{"page_url"=>"google.com", "created_at"=>"2011-08-10T11:00:19Z", "website_id"=>1, "updated_at"=>"2011-08-10T11:00:19Z", "id"=>1}}, {"page"=>{"page_url"=>"blank.com/services/content_services/", "created_at"=>"2011-08-10T11:02:46Z", "website_id"=>1, "updated_at"=>"2011-08-10T11:02:46Z", "id"=>2}}, "respSite"=>{"website"=>{"created_at"=>"2011-08-10T11:00:19Z", "website_id"=>nil, "updated_at"=>"2011-08-10T11:00:19Z", "website_url"=>nil, "id"=>1}}, "respElementTypes"=>{"element_type"=>{"created_at"=>"2011-08-10T11:00:19Z", "updated_at"=>"2011-08-10T11:00:19Z", "id"=>1, "tag_name"=>"head"}}} @respPages, @respSite, @respElementTypes = , , json. Each do |key_category, group_category| group_category. Each do |hash| if group_category.

Is_a? Array eval("@#{key_category}") Is_a? Hash eval("@#{key_category}").

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions