我试图从我的JSON请求中显示哈希的内部级别。
这是JSON请求的返回:
{
"results": [
{
"utc_offset": -25200000,
"venue": {
"zip": "94305",
"country": "us",
"localized_country_name": "USA",
"city": "Stanford",
"address_1": "750 Escondido Road",
"name": "Ray's - Graduate Community Center",
"lon": -122.158386,
"id": 5612552,
"state": "CA",
"lat": 37.423393,
"repinned": false
},
"status": "upcoming"
},
{
"utc_offset": -25200000,
"venue": {
"zip": "94306",
"country": "us",
"localized_country_name": "USA",
"city": "Palo Alto",
"address_1": "25 Churchill Ave",
"name": "Palo Alto High School",
"lon": -122.154121,
"id": 1566333,
"state": "CA",
"lat": 37.433186,
"repinned": false
},
"status": "upcoming"
},
{
"utc_offset": -25200000,
"venue": {
"zip": "94306",
"country": "us",
"localized_country_name": "USA",
"city": "Palo Alto",
"address_1": "25 Churchill Ave",
"name": "Palo Alto High School",
"lon": -122.154121,
"id": 1566333,
"state": "CA",
"lat": 37.433186,
"repinned": false
},
"status": "upcoming"
}
]
}
}
我已提及:How to use JSON data returned by API call in Rails 4,How to get a second level field from json file using ruby,Why do I get "no implicit conversion of String into Integer (TypeError)"?,How to access JSON in Rails?
这帮助我达到了这一点,如果我使用返回@result = @api_response["results"][0]["venue"]["name"]
的{{1}}指定数组索引,我可以显示哈希的内部级别。这就是我想要显示的内容,但我想显示列表中所有场所的名称,而不仅仅是索引[0]上的名称。但我不确定如何遍历索引。
我还可以使用"Ray's - Graduate Community Center"
显示散列结果,并使用:
@result = @api_response["results"]
返回:
<% @result.each do |i| %>
<%= i["venue"] %>
<% end %>
但是当我尝试使用它时:
{"zip"=>"94305", "country"=>"us", "localized_country_name"=>"USA", "city"=>"Stanford", "address_1"=>"750 Escondido Road", "name"=>"Ray's - Graduate Community Center", "lon"=>-122.158386, "id"=>5612552, "state"=>"CA", "lat"=>37.423393, "repinned"=>false}
{"zip"=>"94305", "country"=>"us", "localized_country_name"=>"USA", "city"=>"Stanford", "address_1"=>"750 Escondido Road", "name"=>"Ray's - Graduate Community Center", "lon"=>-122.158386, "id"=>5612552, "state"=>"CA", "lat"=>37.423393, "repinned"=>false}
{"zip"=>"94306", "country"=>"us", "localized_country_name"=>"USA", "city"=>"Palo Alto", "address_1"=>"25 Churchill Ave", "name"=>"Palo Alto High School", "lon"=>-122.154121, "id"=>1566333, "state"=>"CA", "lat"=>37.433186, "repinned"=>false}
{"zip"=>"94306", "country"=>"us", "localized_country_name"=>"USA", "city"=>"Palo Alto", "address_1"=>"25 Churchill Ave", "name"=>"Palo Alto High School", "lon"=>-122.154121, "id"=>1566333, "state"=>"CA", "lat"=>37.433186, "repinned"=>false}
{"zip"=>"94306", "country"=>"us", "localized_country_name"=>"USA", "city"=>"Palo Alto", "address_1"=>"25 Churchill Ave", "name"=>"Palo Alto High School", "lon"=>-122.154121, "id"=>1566333, "state"=>"CA", "lat"=>37.433186, "repinned"=>false}
{"zip"=>"94306", "country"=>"us", "localized_country_name"=>"USA", "city"=>"Palo Alto", "address_1"=>"25 Churchill Ave", "name"=>"Palo Alto High School", "lon"=>-122.154121, "id"=>1566333, "state"=>"CA", "lat"=>37.433186, "repinned"=>false}
我得到<% @result.each do |i| %>
<%= i["venue"]["name"] %> <br />
<% end %>
[]&#39;对于nil:NilClass`错误,它不允许我访问&#34; name&#34;在&#34;场地内#34;这是我想要展示的。
我是否在正确的轨道上?有没有更好的方法来显示哈希的内部级别?任何反馈都会有帮助。谢谢!
答案 0 :(得分:2)
您确定results
数组中的每个对象都有一个venue
字段吗?
在尝试访问venue
字段之前,请确保查看对象是否确实存在。在您的控制器中,添加此
@result = @api_response["results"].reject {|obj| obj.nil?}
这将删除数组中nil
的所有对象。
如果对象存在但字段venue
没有?
<% @result.each do |i| %>
<%= i.has_key? "venue" ? i["venue"]["name"] : 'Location unspecified' %> <br />
<% end %>
如果这看起来很乱,你也可以将它移到辅助方法,如
def venue_name(obj)
obj.has_key? "venue" ? obj["venue"]["name"] : 'Location unspecified'
end
然后在视图中使用它。
<% @result.each do |i| %>
<%= venue_name(i) %> <br/>
<% end %>
答案 1 :(得分:1)
看起来你走在正确的轨道上;只是因为那里的结果是零或者没有其中的场所。
您如何处理问题取决于您希望如何处理这些情况。
您可以过滤结果,以排除没有场地的结果。在查询中执行此操作会更有效,但您可以这样做:
@result.select { |r| r.present? && r.has_key?('venue') }.each do |i|
或者你可以输出&#34;没有场地&#34;或者如果场地不见了。我倾向于在这种情况下使用fetch
。您可以尝试从散列中获取值,但如果散列中的键不是,则返回默认值。所以,在你的情况下,你可以这样做:
<%= i.fetch('venue', {}).fetch('name', 'No venue') %>
这会在结果中查找场地哈希,但如果不在那里则返回空哈希。第二个fetch
然后尝试从该哈希中检索名称,如果找不到,则再次返回'No venue'
。