我正在学习使用API,并开始使用Kimonify从亚马逊提取一些页面数据。在视图中,数据通过@response
变量传递。
如何解析此问题,只显示Author Name
和Rank
#22
?
class HomeController < ApplicationController
def index
require 'rubygems'
require 'json'
require 'rest_client'
@response = RestClient.get 'https://www.kimonolabs.com/api/83x1k5ua?apikey=Gsj16resq87I8wRiaVjOrtzrs0WbAhZr'
end
end
index.html.erb
<h1>Home#index</h1>
<p>Find me in app/views/home/index.html.erb</p>
<p> <%= @response %> </p>
{ "name": "Tim Weed Sales Rank Colonial",
"count": 1,
"frequency":
"Every 15 mins",
"version": 11,
"newdata": true,
"lastrunstatus": "success",
"lastsuccess": "Tue Dec 23 2014 17:28:30 GMT+0000 (UTC)",
"thisversionstatus": "success",
"nextrun": "Tue Dec 23 2014 17:43:30 GMT+0000 (UTC)",
"results": { "Tim Weed Sales Rank Colonial": [ { "Title": "Will Poole's Island [Kindle Edition]",
"Colonial Historical Fiction Rank": "#22" } ]
}
}
答案 0 :(得分:0)
找到解析JSON响应的文章的最新链接。
Convert JSON String to JSON Array in rails?
以下更新
response = RestClient.get 'https://www.kimonolabs.com/api/83x1k5ua?apikey=Gsj16resq87I8wRiaVjOrtzrs0WbAhZr'
采取响应并使用下面的行解析它。然后将解析后的JSON存储在@json变量中,如下所示。
@json = JSON.parse(response)
然后在我的视图中显示这个,我使用下面的代码。 Kimonify将这些项目分为结果,集合和属性等。
<p> <%= @json["results"]["collection1"][0]["property1"]["href"] %> </p>