0
我目前正在尝试在连接到OMBD API的Ruby on Rails中构建一个简单的Web应用程序。我是Ruby的新手和一般编码。我试图运行一个FOR循环来收集搜索查询中的所有标题。这里是我的方法从我查看简单的API调用来解析JSON
def self.findAllTitles(title)
allresponses = []
for page in 1..100 do
url="http://www.omdbapi.com/?s=#{title}&page=#{page}"
response = HTTParty.get(url)
responsebody = JSON.parse(response.body)
allresponses.concat(responsebody)
end
return allresponses []
end
代码:
<% @responsealltitles.each do |result| %>
<td><%= result["Title"] %></td>
<td><%= result["Year"] %></td>
我正试图运行应用程序时出现以下错误:
散列的隐式转换成数组
Extracted source(around line#11): puts responsebody = JSON.parse(response.body)
任何人都可以帮忙吗?
这是一个将要进行的呼叫的示例: –
{“Search”:[{“Title”:“Grand Budapest Hotel”,“Year”:“2014”,“imdbID”:“tt2278388”} ],“totalResults”:“1095”,“Response”:“True”} –
所以我相信这是一个散列,并且存在一个嵌套在散列中的数组,然后在数组中存在多重散列。任何想法如何从这个JSON响应中提取所有哈希? –