开发者

Ruby access hash element

I have the following hash:

{"title"=>"ga:browser=Firefox", "dimensions"=>[{:browser=>"Firefox"}], "metrics"=>[{:pageviews=>25474}], "id"=>"http://www.google.com/analytics/feeds/data?ids=ga:3906565&amp;ga:browser=Firefox&amp;sta开发者_高级运维rt-date=2010-02-06&amp;end-date=2011-02-06", "updated"=>#<DateTime: 212163710400001/86400000,-1/3,2299161>}

How would I print the value of pageviews?


Well, you have a hashmap (not an array) which maps the key "metrics" to an array. That array contains a hash as its only element. And that hash maps the key :pageviews to the value 25474. So to get that value you can do:

the_hash["metrics"][0][:pageviews]

This assumes that the hash with the :pageviews key will always be at position 0 in the array, which the key "metrics" is mapped to.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜