【发布时间】:2018-05-21 00:47:31
【问题描述】:
我有一个包含非营利信息的 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/"
}
]
【问题讨论】:
-
response.each { |r| puts "#{r['name']} #{r['description']}" } -
你正在“隐藏”
response变量。
标签: ruby-on-rails json ruby rails-api