I know theres tutorials on this around the place but I'm having real troubles with this one.
I want to break down a json string into a bunch of smaller objects. I basicaly have 2 servers. One acting as the webapp interface to the whole application and the other is a repository/database.
I'm able to pull down information from the repo to the web app in a json string but after that I don't know how to turn it back.
Heres a sample of the json coming back.
{"respPages":[{"page":{"page_url":"http://www.google.com/","created_at":"2011-08-10T11:00:19Z","website_id":1,"updated_at":"2011-08-10T11:00:19Z","id":1}},{"page":{"page_url":"http://www.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":null,"updated_at":"2011-08-10T11:00:19Z","website_url":null,"id":1}},"respElementTypes":[{"element_type":{"created_at":"2011-08-10T11:00:19Z","updated_at":"2011-08-10T11:00:19Z","id":1,"tag_name":"head"}},
Basically there are 4 tags in the json.
page website elementType elementData
I would like to do something like create 4 arrays and populate them with the object that matches these tags but havent the first inkling as to how to do it.
I would imaging its something like this:
#Get the json from repo using net/http
uri = URI.parse("http://127.0.0.1:3007/repository/infoid/1.json")
http = Net::HTTP.new(uri.host, uri.port)
response = http.request(Net::HTTP::Get.new(uri.request_uri))
@x = response.to_hash
@pages = Array.new
@websites= Array.new
@elementDatas = Array.new
@elementTypes = Array.new
#enter code here`#For every bit of the hash, find out what it is and allocate it accordingly
@x.each_with_index do |e,index|
if e.tagName == pages #Getting real javascripty here. There must be someway to check the tag or title of the element
@pages[index]=e
end
Desired output as requested by apneadiving
The desired output is to have 4 arrays each containing different types of objects. Eg
@pagesArray[1]
should contain the first occurrence of a page object in the json string.
then do the same for the other ones. Of course I;d need to break down the object further but once I can break down the top level and categorize them, then I can go deeper.
So how do i turn Json back to objects in ruby and reference them (something like tag name?)?
P.s In the JSON there is already a tag title respPages and respWebsites which is basically all the objects grouped.
ActiveSupport::JSON.decode(string)
– apneadiving Aug 10 '11 at 15:17