dubstep
September 5, 2011, 5:47pm
1
Hi folks,
I have the following structure /json like:
{
-
items: [
-
{
google_analytics_id: null
title: "abc"
type: "station"
masthead_file: null
slug: "abcd"
}
-
{
google_analytics_id: null
title: "xyz"
type: "station"
masthead_file: null
slug: "xyzt"
}
etc.
I want to iterate the array of hashes and return for example the
second hash or return the value from the second hash for title key.
Can you provide some guidelines ?
Thank you,
Dan
That isn’t valid json syntax nor is it valid ruby syntax. Are you
trying to parse a file with that
syntax?
my_array.select{|item| item[:key] == my_criterion}
…will give you an array of items that meet your needs if the ‘item’
is a hash.
I already parsed the json call. The idea is that i want to access a
certain hash within :item => [] or return the slug value for title =>
“xyz”
Well, it looks to me like you have an outer hash with key/values, and a
value is an array of hashes, so you would do this:
main_hash[“items”][1][“title”] ==> ‘xyz’
^ ^
| |
array |
|
hash
Or,
outer_hash.values.each do |array|
array.each do |hash|
#do something with hash
end
end