简体   繁体   English

Ruby访问哈希元素

[英]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;start-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. 好吧,你有一个hashmap(不是一个数组),它将关键的"metrics"映射到一个数组。 That array contains a hash as its only element. 该数组包含一个哈希作为其唯一元素。 And that hash maps the key :pageviews to the value 25474 . 并且该哈希将关键点:pageviews映射到值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. 这假设带有:pageviews键的散列将始终位于数组中的位置0,键"metrics"将映射到该位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM