Parsing JSON response into ruby data structure.

1,097 views
Skip to first unread message

kanishk

unread,
Dec 24, 2009, 4:54:29 AM12/24/09
to rhomobile
Hi,

I am very new to Rhodes and Ruby.I am struggling to find out how to
parse a JSON response returned from a webservice using Ruby in Rhodes.
To understand this,I wrote the following code in the controller.rb
file of a model.

require 'rubygems'
require 'json'
require 'net/http'

def news_search(query, results=10, start=1)
base_url = "http://search.yahooapis.com/NewsSearchService/V1/
newsSearch?appid=YahooDemo&output=json"
url = "#{base_url}&query=#{URI.encode(query)}&results=#{results}
&start=#{start}"
resp = Net::HTTP.get_response(URI.parse(url))
data = resp.body

# we convert the returned JSON data to native Ruby
# data structure - a hash
result = JSON.parse(data)

# if the hash has 'Error' as a key, we raise an error
if result.has_key? 'Error'
raise "web service error"
end
return result
end

In implementing this i don't know how to call this parametrized method
( news_search(query, results=10, start=1) ) from Index.erb file.
I wanted to know if this approach is correct and if its not corect how
should i get going?

If posible,Please provide some sample application or code snippet to
achieve this.

evgeny vovchenko

unread,
Dec 24, 2009, 2:07:57 PM12/24/09
to rhom...@googlegroups.com
Erb is ruby file, so you can call any ruby methods from there(read doc about
erb)
But usually erb uses as a view, so you prepare some data in controller, save
it in instance variable and call render. See rhodes-store as an example or
any other rhomobile app

Hi,

--

You received this message because you are subscribed to the Google Groups
"rhomobile" group.
To post to this group, send email to rhom...@googlegroups.com.
To unsubscribe from this group, send email to
rhomobile+...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rhomobile?hl=en.


kanishk

unread,
Dec 28, 2009, 4:41:47 AM12/28/09
to rhomobile
Thanks for your reply.But i am still unable to parse a json response
into ruby data structure.
It would be a great help for me if u provide me some code snippet or
some some application for doing so.

Thanks and Regards,
Kanishk

On Dec 25, 12:07 am, "evgeny vovchenko" <evgenyvovche...@gmail.com>
wrote:

> For more options, visit this group athttp://groups.google.com/group/rhomobile?hl=en.- Hide quoted text -
>
> - Show quoted text -

evgeny vovchenko

unread,
Dec 28, 2009, 5:08:19 AM12/28/09
to rhom...@googlegroups.com
See system-api-samples\app\HttpTest\controller.rb

kanishk

unread,
Dec 29, 2009, 6:49:16 AM12/29/09
to rhomobile
checked the system-api-samples\app\HttpTest\controller.rb which was
definitely very useful in understanding how the parsing is done and
what result we get after parsing.Here,i found out that parsing of a
static JSON reponse is done as follows:

res = JSON.parse("[{\"count\":10},{\"version\":1},{\"total_count\":
5425},{\"token\": 123},{\"s\":\"RhoDeleteSource\",\"ol\":[{\"o\":
\"rho_del_obj\",\"av\":[{\"i\":55550425},{\"i\":75665819},{\"i\":
338165272},{\"i\":402396629},{\"i\":521753981},{\"i\":664143530},{\"i
\":678116186},{\"i\":831092394},{\"i\":956041217},{\"i\":
970452458}]}]}]")

So,static JSON response is passed in JSON.parse.But what i needed is
to first return a JSON response from any webservice and then parsing
this reponse. How can i achieve this? Can i have a sample code where
we are first returning a JSON response from a webservice and then
parsing this response?

Thanks and Regards
Kanishk


On Dec 28, 3:08 pm, "evgeny vovchenko" <evgenyvovche...@gmail.com>
wrote:


> See system-api-samples\app\HttpTest\controller.rb
>
>
>
> -----Original Message-----
> From: rhom...@googlegroups.com [mailto:rhom...@googlegroups.com] On
>
> Behalf Of kanishk
> Sent: Monday, December 28, 2009 12:42 PM
> To: rhomobile
> Subject: [rhomobile] Re: Parsing JSON response into ruby data structure.
>
> Thanks for your reply.But i am still unable to parse a json response into
> ruby data structure.
> It would be a great help for me if u provide me some code snippet or some
> some application for doing so.
>
> Thanks and Regards,
> Kanishk
>

> On Dec 25, 12:07šam, "evgeny vovchenko" <evgenyvovche...@gmail.com>


> wrote:
> > Erb is ruby file, so you can call any ruby methods from there(read doc
> > about
> > erb)
> > But usually erb uses as a view, so you prepare some data in
> > controller, save it in instance variable and call render. See
> > rhodes-store as an example or any other rhomobile app
>
> > -----Original Message-----
> > From: rhom...@googlegroups.com [mailto:rhom...@googlegroups.com]
> > On
>
> > Behalf Of kanishk
> > Sent: Thursday, December 24, 2009 12:54 PM
> > To: rhomobile
> > Subject: [rhomobile] Parsing JSON response into ruby data structure.
>
> > Hi,
>
> > I am very new to Rhodes and Ruby.I am struggling to find out how to
> > parse a JSON response returned from a webservice using Ruby in Rhodes.
> > To understand this,I wrote the following code in the controller.rb
> > file of a model.
>
> > require 'rubygems'
> > require 'json'
> > require 'net/http'
>
> > def news_search(query, results=10, start=1)

> > š šbase_url = "http://search.yahooapis.com/NewsSearchService/V1/
> > newsSearch?appid=YahooDemo&output=json"
> > š šurl = "#{base_url}&query=#{URI.encode(query)}&results=#{results}
> > &start=#{start}"
> > š šresp = Net::HTTP.get_response(URI.parse(url))
> > š šdata = resp.body
>
> > š š# we convert the returned JSON data to native Ruby
> > š š# data structure - a hash
> > š šresult = JSON.parse(data)
>
> > š š# if the hash has 'Error' as a key, we raise an error
> > š šif result.has_key? 'Error'
> > š š š raise "web service error"
> > š šend
> > š šreturn result


> > end
>
> > In implementing this i don't know how to call this parametrized method
> > ( news_search(query, results=10, start=1) ) from Index.erb file.
> > I wanted to know if this approach is correct and if its not corect how
> > should i get going?
>
> > If posible,Please provide some sample application or code snippet to
> > achieve this.
>
> > --
>
> > You received this message because you are subscribed to the Google
> > Groups "rhomobile" group.
> > To post to this group, send email to rhom...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > rhomobile+...@googlegroups.com.
> > For more options, visit this group

> > athttp://groups.google.com/group/rhomobile?hl=en.-Hide quoted text -

evgeny vovchenko

unread,
Dec 29, 2009, 7:00:50 AM12/29/09
to rhom...@googlegroups.com
Something like this:
uri = URI.parse("http://www.example.com")
request = Net::HTTP::Get.new("/webservices/test", 'Accept' =>
'application/json')
response = Net::HTTP.start(uri.host,uri.port) do |http|
http.set_debug_output $stderr
http.request(request)
end
Reply all
Reply to author
Forward
0 new messages