Ruby: Parsing a complex hash
I have a hash that goes multiple levels deep: http://gist.github.com/285350
I am trying 开发者_C百科to loop through each serving but I keep running into multiple nil[] errors even though the hash isn't nil.
For example:
food_hash["food"]["servings"]
Returns nil.[]
It might be because im half asleep but I can't seem to get down to the "serving_description"... could anyone help put me in the right direction?
I can't see get down to the "serving_description"
Note that (1) servings.serving
is an array, and (2) food
is not a key in the hash. Try this instead:
f["servings"]["serving"][0]["serving_description"]
=> "1 thin slice (yield after cooking)"
where f
is the hash.
Is this what you're trying to do?
food_hash["servings"]["serving"][0]["serving_description"]
精彩评论