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 [email protected]
@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
Marko A. 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 [email protected]
@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 [email protected]
@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
On Nov 23, 7:30 pm, Marko A. [email protected] wrote:
@structs = JSON.parse(@json_stream)
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 – 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.
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
On Nov 24, 8:05 am, 7stud – [email protected] wrote:
{“u”:“http://…html”,“d”:“some title”,“t”:[“ruby”]},
if key == ‘t’
json
library
Posted viahttp://www.ruby-forum.com/.
Thanks a lot 7stud. I didn’t realize that each val (as in your last
code snippet) is an array.