我有一个包含公益信息的API,我试图根据"类别"来获取结果。参数。基本上,我想循环并返回该类别下每个非营利组织的名称,描述,地址,电话号码和网站。我一直得到"没有将String隐式转换为整数"错误或"未定义的方法[]"我一直在采取的方法出错。非常感谢帮助!
控制器:
def index
@homeless_organizations = Organization.get_homeless_organizations
end
服务
class Organization
def self.get_homeless_organizations
response = JSON.parse(RestClient.get('http://localhost:3001/organizations?
category=Homelessness'))
response['name']['description']['address']['phone']['website'].each do
|response|
puts response
puts "#{response['name']['description']['address']['phone']
['website']}"
end
end
JSON正文:
[
{
"id": 1,
"name": "Transition Projects",
"category": "Homelessness",
"description": "Transition Projects delivers life-saving and life-changing assistance to some of Portland’s most vulnerable residents. Whether by helping a homeless veteran and her family find housing, sheltering hundreds of people each night with nowhere else to turn, or opening new pathways to employment, Transition Projects represents an invaluable part of Portland’s social fabric.",
"address": "665 NW Hoyt St, Portland, OR 97209",
"phone": "(503) 280-4700",
"created_at": "2018-05-19T00:18:30.592Z",
"updated_at": "2018-05-19T00:18:30.592Z",
"website": "https://www.tprojects.org/"
},
{
"id": 2,
"name": "Portland Homeless Family Solutions",
"category": "Homelessness",
"description": "Giving hope to homeless families with children. Our mission is to empower homeless families with children to get back into housing - and stay there.",
"address": "1221 SW Yamhill St #210, Portland, OR 97205",
"phone": "(503) 915-8306",
"created_at": "2018-05-19T00:18:30.600Z",
"updated_at": "2018-05-19T00:18:30.600Z",
"website": "http://pdxhfs.org/"
},
{
"id": 3,
"name": "Portland Rescue Mission",
"category": "Homelessness",
"description": "Food, shelter, clothing, mail service, restrooms, showers and other emergency services are available free of charge at Portland Rescue Mission's Burnside Shelter.",
"address": "111 W Burnside St, Portland, OR 97209",
"phone": "(503) 906-7690",
"created_at": "2018-05-19T00:18:30.603Z",
"updated_at": "2018-05-19T00:18:30.603Z",
"website": "https://www.portlandrescuemission.org/.../food-shelter-services/"
}
]
答案 0 :(得分:1)
解析JSON响应后,最终会出现一系列哈希值。要从所有散列中获取值,您需要遍历该数组并在每个散列上为所需的每个值调用Hash#[]
方法。
response.each do |organization|
p [organizations['name'], organization['description']]
end
由于您需要哈希值中的多个值,而不是一次调用一个值(如上所述),您可以使用Hash#values_at
执行类似于您尝试的操作并获取所有内容在一个新数组中立即输出值:
response.each do |organization|
p organization.values_at('name', 'description')
end
关于您的代码有什么问题:
调用response['name']
调用的Array#[]
与Hash#[]
不同,它需要一个整数索引来表示您想要访问的元素,所以如果您只想获取值来自你可以做的第一个组织:
p [response[0]['name'], response[0]['description']]
但是你传递了一个字符串,它没有预料到,所以你得到了TypeError (no implicit conversion of String into Integer)
。 (这只是一个例子,使用each
比传递特定索引更好,除非您只需要特定的索引。)
最后,当你一个接一个地跟着#[]
进行呼叫时,就像你正在做的那样,在第一个之后,你最终会在前一个呼叫的返回值上调用#[]
。在这种情况下,如果您在数组中的散列而不是数组本身上调用了#[]
,那么您最终会调用String#[]
,这与Array#[]
不同, Hash#[]
。虽然它可以采用字符串或整数,但它不会做你想要的(如果给出一个字符串,如果它在你调用#[]
的字符串中存在则返回该字符串;如果是整数则是如果它返回字符串中该位置的字符):
# "Transition Projects" is `response[0]['name']`
"Transition Projects"['description'] # => nil
"Transition Projects"['Proj'] # => "Proj"
"Transition Projects"[8] # => "o"
然后您最终在#[]
(nil
)或其他字符串上调用NoMethodError
。