如何从此哈希中访问值?

时间:2019-04-24 20:25:52

标签: ruby-on-rails ruby

我正在尝试访问此哈希中的latlon值,但是当我调用@object["lat"]object[:lat]时,它将返回nil。这是哈希值:

{"searchresults"=>{"place"=>{"place_id"=>"100066", "osm_type"=>"node", "osm_id"=>"107775", "place_rank"=>"15", "boundingbox"=>"51.3473219,51.6673219,-0.2876474,0.0323526", "lat"=>"51.5073219", "lon"=>"-0.1276474", "display_name"=>"London, Greater London, England, SW1A 2DX, United Kingdom", "class"=>"place", "type"=>"city", "importance"=>"0.9654895765402", "icon"=>"https://locationiq.org/static/images/mapicons/poi_place_city.p.20.png"}, "timestamp"=>"Thu, 25 Apr 19 01:43:32 +0530", "attribution"=>"https://locationiq.com/attribution", "querystring"=>"london", "polygon"=>"false", "exclude_place_ids"=>"100066", "more_url"=>"https://locationiq.org/static/search.php?q=london&exclude_place_ids=100066&format=xml"}}

4 个答案:

答案 0 :(得分:4)

如果您有:

object = {"searchresults"=>{"place"=>{"place_id"=>"100066", "osm_type"=>"node", "osm_id"=>"107775", "place_rank"=>"15", "boundingbox"=>"51.3473219,51.6673219,-0.2876474,0.0323526", "lat"=>"51.5073219", "lon"=>"-0.1276474", "display_name"=>"London, Greater London, England, SW1A 2DX, United Kingdom", "class"=>"place", "type"=>"city", "importance"=>"0.9654895765402", "icon"=>"https://locationiq.org/static/images/mapicons/poi_place_city.p.20.png"}, "timestamp"=>"Thu, 25 Apr 19 01:43:32 +0530", "attribution"=>"https://locationiq.com/attribution", "querystring"=>"london", "polygon"=>"false", "exclude_place_ids"=>"100066", "more_url"=>"https://locationiq.org/static/search.php?q=london&exclude_place_ids=100066&format=xml"}}

您可能要使用:

object.dig('searchresults','place','lat')
 => "51.5073219"

好处是,如果您的object没有预期的结构,则会收到nil

object.dig('searchresults','foo_place','lat')
 => nil 

顺序哈希访问器将引发错误:

object['searchresults']['foo_place']['lat']
Traceback (most recent call last):
        1: from (irb):60
NoMethodError (undefined method `[]' for nil:NilClass)

nil相比,NoMethodError的处理可能要容易得多。

如果您更喜欢使用符号(而不是字符串)作为键,则可以执行以下操作:

object.with_indifferent_access.dig(:searchresults, :place, :lat)
 => "51.5073219"

请注意,如果没有with_indifferent_access(或将键从字符串转换为符号的另一种方法):

object[:searchresults][:place][:lat]
Traceback (most recent call last):
        1: from (irb):67
NoMethodError (undefined method `[]' for nil:NilClass)

答案 1 :(得分:1)

@object["searchresults"]["place"]["lat"]

答案 2 :(得分:1)

如果您有此代码,则可以通过以下方式检索latlon

object = {"searchresults"=>{"place"=>{"place_id"=>"100066", "osm_type"=>"node", "osm_id"=>"107775", "place_rank"=>"15", "boundingbox"=>"51.3473219,51.6673219,-0.2876474,0.0323526", "lat"=>"51.5073219", "lon"=>"-0.1276474", "display_name"=>"London, Greater London, England, SW1A 2DX, United Kingdom", "class"=>"place", "type"=>"city", "importance"=>"0.9654895765402", "icon"=>"https://locationiq.org/static/images/mapicons/poi_place_city.p.20.png"}, "timestamp"=>"Thu, 25 Apr 19 01:43:32 +0530", "attribution"=>"https://locationiq.com/attribution", "querystring"=>"london", "polygon"=>"false", "exclude_place_ids"=>"100066", "more_url"=>"https://locationiq.org/static/search.php?q=london&exclude_place_ids=100066&format=xml"}}
lat = object["searchresults"]["place"]["lat"]
lon = object["searchresults"]["place"]["lon"]

答案 3 :(得分:0)

一旦正确缩进,就更容易看到哈希结构。因此,这是您要访问的哈希的结构:

{
  "searchresults"=> {
    "place"=> {
      "place_id"=>"100066", 
      "osm_type"=>"node", 
      "osm_id"=>"107775", 
      "place_rank"=>"15", 
      "boundingbox"=>"51.3473219,51.6673219,-0.2876474,0.0323526", 
      "lat"=>"51.5073219", 
      "lon"=>"-0.1276474", 
      "display_name"=>"London, Greater London, England, SW1A 2DX, United Kingdom", 
      "class"=>"place", "type"=>"city", "importance"=>"0.9654895765402", 
      "icon"=>"https://locationiq.org/static/images/mapicons/poi_place_city.p.20.png"
    }, 
    "timestamp"=>"Thu, 25 Apr 19 01:43:32 +0530", "attribution"=>"https://locationiq.com/attribution", 
    "querystring"=>"london", "polygon"=>"false", "exclude_place_ids"=>"100066", 
    "more_url"=>"https://locationiq.org/static/search.php?q=london&exclude_place_ids=100066&format=xml"
  }
}

从这里,您可以看到lat键实际上在searchresults -> place -> lat路径下。因此,您可以使用

@object['searchresults']['place']['lat']

@object.dig('searchresults', 'place', 'lat')