gpt4 book ai didi

ruby-on-rails - Rails 如何显示来自 JSON 请求的哈希值

转载 作者:数据小太阳 更新时间:2023-10-29 08:43:44 24 4
gpt4 key购买 nike

我正在尝试显示来 self 的 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"] 返回 “Ray's - Graduate Community Center”。这就是我要显示的内容,但我想显示列表中所有地点的名称,而不仅仅是索引 [0] 处的名称。但我不确定如何遍历索引。

我还可以使用 @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 %>

我得到一个 undefined method[]' for nil:NilClass` 错误,它不允许我访问我试图显示的“地点”内的“名称”。

我走在正确的 rails 上吗?有没有更好的方法来显示哈希的内部级别?任何反馈都会有所帮助。谢谢!

最佳答案

您确定 results 数组中的每个对象都有一个 venue 字段吗?

在尝试访问 venue 字段之前,请确保查看该对象是否实际存在。在你的 Controller 中,添加这个

@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

然后在 View 中使用它。

 <% @result.each do |i| %>
<%= venue_name(i) %> <br/>
<% end %>

关于ruby-on-rails - Rails 如何显示来自 JSON 请求的哈希值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39379219/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com