From: "Kevin K." Date: 2013-01-12T08:34:23+09:00 Subject: Re: Httparty parsed response 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 -- Posted via http://www.ruby-forum.com/.