[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Parsing nested JSON data

Marko Anastasov

11/24/2007 12:26:00 AM

Hi,
I didn't find a mailing list dedicated to ruby-json, so I assume that
this would be
the correct place to ask.

From a web API, I receive a JSON stream such as:

[{"u":"http://...html","d":"some title","t":["ruby"]},
{"u":"http://....org/","d":"another title","t":
["ruby","json","library"]},...]

Now, among the rest, I also want to access those "ruby", "json" and
"library" strings.
With the following code:

@structs = JSON.parse(@json_stream)

for i in 0...@structs.length
@struct_obj = @structs.fetch(i)
@struct_obj.each { |key, value|
if key == "t"
# get the value that 't' points to
end
}
end

I get them all appended to each other without any whitespace, ie
"rubyjsonlibrary".

How could I access them individually?

Marko
5 Answers

7stud --

11/24/2007 1:49:00 AM

0

Marko Anastasov wrote:
> Now, among the rest, I also want to access those "ruby", "json" and
> "library" strings.
> With the following code:
>
> @structs = JSON.parse(@json_stream)
>
> for i in 0...@structs.length
> @struct_obj = @structs.fetch(i)
> @struct_obj.each { |key, value|
> if key == "t"
> # get the value that 't' points to
> end
> }
> end
>
> I get them all appended to each other without any whitespace, ie
> "rubyjsonlibrary".
>
> How could I access them individually?
>
> Marko

> for i in 0...@structs.length
> @struct_obj = @structs.fetch(i)
> @struct_obj.each { |key, value|
> if key == "t"
> # get the value that 't' points to

#the value that t points to is stored in your 'value' variable
value.each do |str|
puts str
end

> end
> }
> end
--
Posted via http://www.ruby-....

Trans

11/24/2007 3:37:00 AM

0



On Nov 23, 7:30 pm, Marko Anastasov <marko.anasta...@gmail.com> wrote:
> From a web API, I receive a JSON stream such as:
>
> [{"u":"http://...html","d":"some title","t":["ruby"]},
> {"u":"http://....org/","d":"another title","t":
> ["ruby","json","library"]},...]
>
> Now, among the rest, I also want to access those "ruby", "json" and
> "library" strings.
> With the following code:
>
> @structs = JSON.parse(@json_stream)
>
> for i in 0...@structs.length
> @struct_obj = @structs.fetch(i)
> @struct_obj.each { |key, value|
> if key == "t"
> # get the value that 't' points to
> end
> }
> end
>
> I get them all appended to each other without any whitespace, ie
> "rubyjsonlibrary".
>
> How could I access them individually?

7stud probably covered your question, so if you don't mind I'd like to
hijack this thread and ask a more general question about JSON
parsing...

Is it possible that JSON could be built into Ruby? The syntax is so
close to Ruby's as of 1.9, that it seems a small step and rather a
shame not to just go ahead and make it compatible. Off hand it seems
that only quoted keys are missing.

T.

7stud --

11/24/2007 4:06:00 AM

0

7stud -- wrote:
> #the value that t points to is stored in your 'value' variable
> value.each do |str|
> puts str
> end

Whoops. Never mind. I'm not even sure why anyone would use the
ruby-json gem. There's no documentation anywhere, and after I installed
it, I couldn't even require it into a program without error.
--
Posted via http://www.ruby-....

7stud --

11/24/2007 7:05:00 AM

0

7stud -- wrote:
> There's no documentation anywhere, and after I installed
> it, I couldn't even require it into a program without error.

Look how easy it is with the json gem:

require 'rubygems'
require 'json'

str = '[
{"u":"http://...html","d":"some title","t":["ruby"]},
{"u":"http://....org/","d":"another
title","t":["ruby","json","library"]}
]'

arr = JSON.parse(str)
p arr
puts

target_hash = arr[1]
target_hash.each do |key, val|
if key == 't'
val.each {|elmt| puts elmt}
end
end


--output:--
[{"d"=>"some title", "t"=>["ruby"], "u"=>"http://...html"},
{"d"=>"another title", "t"=>["ruby", "json", "library"],
"u"=>"http://....org/"}]

ruby
json
library
--
Posted via http://www.ruby-....

Marko Anastasov

11/24/2007 11:08:00 AM

0

On Nov 24, 8:05 am, 7stud -- <bbxx789_0...@yahoo.com> wrote:
> 7stud -- wrote:
> > There's no documentation anywhere, and after I installed
> > it, I couldn't even require it into a program without error.
>
> Look how easy it is with the json gem:
>
> require 'rubygems'
> require 'json'
>
> str = '[
> {"u":"http://...html","d":"some title","t":["ruby"]},
> {"u":"http://....org/","d":"another
> title","t":["ruby","json","library"]}
> ]'
>
> arr = JSON.parse(str)
> p arr
> puts
>
> target_hash = arr[1]
> target_hash.each do |key, val|
> if key == 't'
> val.each {|elmt| puts elmt}
> end
> end
>
> --output:--
> [{"d"=>"some title", "t"=>["ruby"], "u"=>"http://...html"},
> {"d"=>"another title", "t"=>["ruby", "json", "library"],
> "u"=>"http://....org/"}]
>
> ruby
> json
> library
> --
> Posted viahttp://www.ruby-....

Thanks a lot 7stud. I didn't realize that each val (as in your last
code snippet) is an array.