我有一个包含非营利信息的API,我正在尝试根据"类别"参数从中获取结果。基本上,我想遍历并返回该类别下每个非营利组织的名称、描述、地址、电话号码和网站。我一直在采用的方法中不断收到"没有将字符串隐式转换为整数"错误或"未定义的方法 []"错误。帮助将不胜感激!
控制器:
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/"
}
]
解析 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']
是调用与Hash#[]
不同的Array#[]
,它需要一个整数索引,您要访问哪个元素,因此,如果您只想从第一个组织获取值,则可以这样做:
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
(上调用#[]
,要么在另一个字符串上调用。