Hi all,
I am using httparty to get the content of a service.
@reponse = httparty.get(url)
puts @reponse.parsed_response
parsed_reponse is as follows,
@parsed_response =>{"name" => "ruby", "number" => {"item"=>{"@no"=>"1",
"itemcontent"=>{"itemcategory"=>{"@itemtype"=>"Book",
"@itemtag"=>"Generak"}}, "form"=>"text", "shape"=>"square",
"dimension"=>"full", "type"=>{"category"=>{"@itemid"=>"24",
"@itemexpiry"=>"false", "@quote"=>"[FRESH]", "$"=>"2010"}},
"expiryComments"=>{"expiryStatus"=>{"@expiry"=>"true", "$"=>"2012"}}},
"shop"=>{"shopName"=>"STORE"}}}
I need to parse each and every hash and its elements and even array of
elements inside each hash
for example say,
I need to parse and equate,
name = "rub"
@parsed_response["name"].should == name
this will result
expected: rub
got: ruby
similarly, for each and every hash I need to check the value with some
other value, whether they are equal or not.
Can anyone kindly explain with code, how to parse each value of the
above response.
the above parsed response is only one such record, but i do have
multiple records to check like
@parsed_response => {"name" => "ruby", "number" => {"item"=>{"@no"=>"1",
.......}},{"item" => {"@no" = >"2", ....}},{"item" => {"@no" = >"3",
....}}}
In this case I need to check for all the values. need assistance for
this.
Thanks in advance
on 2011-12-09 21:16
on 2013-01-12 00:34
rubythemystery ruby wrote in post #1036005: > Hi all, > > I am using httparty to get the content of a service. > > > @reponse = httparty.get(url) > > puts @reponse.parsed_response > > parsed_reponse is as follows, > > @parsed_response =>{"name" => "ruby", "number" => {"item"=>{"@no"=>"1", > "itemcontent"=>{"itemcategory"=>{"@itemtype"=>"Book", > "@itemtag"=>"Generak"}}, "form"=>"text", "shape"=>"square", > "dimension"=>"full", "type"=>{"category"=>{"@itemid"=>"24", > "@itemexpiry"=>"false", "@quote"=>"[FRESH]", "$"=>"2010"}}, > "expiryComments"=>{"expiryStatus"=>{"@expiry"=>"true", "$"=>"2012"}}}, > "shop"=>{"shopName"=>"STORE"}}} > > I need to parse each and every hash and its elements and even array of > elements inside each hash > > for example say, > > I need to parse and equate, > > name = "rub" > > @parsed_response["name"].should == name > > this will result > expected: rub > got: ruby > > similarly, for each and every hash I need to check the value with some > other value, whether they are equal or not. > > Can anyone kindly explain with code, how to parse each value of the > above response. > > the above parsed response is only one such record, but i do have > multiple records to check like > > @parsed_response => {"name" => "ruby", "number" => {"item"=>{"@no"=>"1", > .......}},{"item" => {"@no" = >"2", ....}},{"item" => {"@no" = >"3", > ....}}} > > In this case I need to check for all the values. need assistance for > this. > > > Thanks in advance I'm still a bit of a nube with ruby but I have been working with this library a little bit. Using your example response: def check_and_change res=@parsed_response name='rub' itemtype='Book' # if the name in your results does not equal 'rub' changes the value to 'rub' if res['name'] != name res['name']=name end #if the item type is 'Book' puts 'Win' if res['number']['item']['itemcontent']['itemcatagory']['@itemtype']== itemtype puts 'Win' end #If there are multiple 'item' records assuming it looks like this: > @parsed_response => {"name" => "ruby", "number" => {"item"=>{"@no"=>"1", > .......}},{"item" => {"@no" = >"2", ....}},{"item" => {"@no" = >"3", > ....}}} if res['number'].first['item']['itemcontent']['itemcatagory']['@itemtype']== itemtype puts 'first Win' end if res['number'].last['item']['itemcontent']['itemcatagory']['@itemtype']== itemtype puts 'last Win' end Hope that makes sense and helps
on 2013-01-14 20:13
rubythemystery ruby wrote in post #1036178:
> Can Any one Help me on this
The response is in standard JSON format. Look into the JSON gem (IIRC,
included in most default ruby installs). The method you'll want
specifically is "JSON.parse @reponse.parsed_response". That will return
the JSON data in native ruby format as hashes, arrays, strings, etc.
From there, you can manipulate the data using those objects' methods.
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.