如何获取多个JSON URL并解析动态内容

如何获取多个JSON URL并解析动态内容,json,ruby,sinatra,net-http,open-uri,Json,Ruby,Sinatra,Net Http,Open Uri,我正在尝试创建第一个从TMDB的API解析数据的应用程序 当我尝试加载和解析一个URL时,一切正常: uri = URI.parse("http://api.themoviedb.org/3/tv/#URI.escape(@id)}?api_key=#{@api_key}") http = Net::HTTP.new(uri.host, uri.port) request = Net::HTTP::Get.new(uri.request_uri) response = http.request(

我正在尝试创建第一个从TMDB的API解析数据的应用程序

当我尝试加载和解析一个URL时,一切正常:

uri = URI.parse("http://api.themoviedb.org/3/tv/#URI.escape(@id)}?api_key=#{@api_key}")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
@results = JSON.load(response.body)
但现在我想解析JSON,可从以下网址获得:

/tv/{tv_id}/season/{season_number}/episode/{episode_number}
其中:
{tv_id}
@id=params[:id]

{season\u number}
{season\u number}
是由第一个URL生成的季节和集。 我尝试了多种方法,但每次都会出现随机错误

完整代码已打开。 正在运行的应用程序是

我试图在同一个页面上显示赛季标题和剧集。 例如,当我解析时:

https://api.themoviedb.org/3/tv/2432?api_key=**********
我得到:

{
"backdrop_path":"/hl9mC6fc2adfeGpI1ijKCfQ0KzI.jpg",
"name":"Taken",
"number_of_episodes":10,
"number_of_seasons":1
}
所以我一季有10集。对于每一集,我都有一个URL,将集号
{1..5..10}
替换为:

https://api.themoviedb.org/3/tv/8681/season/1/episode/*1*?api_key=********
我想显示每一集的集名,因此一集的JSON结果如下:

{
"name":"Beyond the Sky","overview":"......",
"id":183273,
"vote_average":0.0,
"vote_count":0
}

对于该请求,您需要使用https,请参阅我用#**标记的需要更改的部分。请注意使用Net::HTTP.start而不是Net::HTTP.new。 顺便说一下,这是一款不错的应用程序

require 'net/http'
require 'net/https' #*add*
require 'json'
require 'open-uri'

url = 'https://api.themoviedb.org/3/tv/1418/season/1/episode/1?api_key=**************************&language=en-US'
uri = URI.parse(url)
# *adapt following line*
http = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https', :verify_mode => OpenSSL::SSL::VERIFY_NONE)
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
json = JSON.load(response.body)


# {"air_date"=>"2007-09-24", "crew"=>[{"id"=>157437, "credit_id"=>"5256cfee19c2956ff60a280c", "name"=>"James Burrows", "department"=>"Directing", "job"=>"Director", "profile_path"=>"/lTcRumFOm6HkfOyPuUElV4l4n4r.jpg"}, {"id"=>160172, "credit_id"=>"5256cfbc19c2956ff60a0483", "name"=>"Chuck Lorre", "department"=>"Writing", "job"=>"Writer", "profile_path"=>"/btpYlMV71sjQXrV142I9kogEINI.jpg"}, {"id"=>163528, "credit_id"=>"5256cfbd19c2956ff60a04f0", "name"=>"Bill Prady", "department"=>"Writing", "job"=>"Writer", "profile_path"=>"/duXUvo8JtivQR0BHiXHGQwoNYB4.jpg"}, {"id"=>1480308, "credit_id"=>"55881d379251416818001c2b", "name"=>"Janice Zoladz", "department"=>"Costume & Make-Up", "job"=>"Hairstylist", "profile_path"=>nil}, {"id"=>1480289, "credit_id"=>"558819ffc3a36836ea006145", "name"=>"Joe Bella", "department"=>"Production", "job"=>"Co-Producer", "profile_path"=>nil}, {"id"=>1232374, "credit_id"=>"55881a1492514179f6003f6e", "name"=>"Michael Collier", "department"=>"Production", "job"=>"Producer", "profile_path"=>nil}, {"id"=>1480291, "credit_id"=>"55881a269251411efc000f6e", "name"=>"Toti Levine", "department"=>"Production", "job"=>"Associate Producer", "profile_path"=>nil}], "episode_number"=>1, "guest_stars"=>[{"id"=>1118085, "name"=>"Brian Patrick Wade", "credit_id"=>"5256cfc919c2956ff60a0c8f", "character"=>"Kurt", "order"=>0, "profile_path"=>"/6y0Hd1xLC5Nitedg2GQ90DtxxDb.jpg"}, {"id"=>157085, "name"=>"Vernee Watson-Johnson", "credit_id"=>"5256cfd719c2956ff60a1747", "character"=>"", "order"=>1, "profile_path"=>"/dRXXPoeAAbl1PhURCJfKCDoZiUn.jpg"}], "name"=>"Pilot", "overview"=>"Brilliant physicist roommates Leonard and Sheldon meet their new neighbor Penny, who begins showing them that as much as they know about science, they know little about actual living.", "id"=>64766, "production_code"=>nil, "season_number"=>1, "still_path"=>"/rxWlBXZhGWhumbLB8gAHyyW3ITD.jpg", "vote_average"=>7.39285714285714, "vote_count"=>14}
编辑:要获得一季的所有剧集,你只需从1到剧集数进行计数,然后组合并请求新的url。 你将不得不做同样的事情来列举一个系列的所有季节,但我留给你一些工作超过8:)成功

require 'net/http'
require 'net/https'
require 'json'
require 'open-uri'
require 'pp'

# we have to do this more than once, so let's make it DRY
def get_json url
  uri = URI.parse(url)
  http = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https', :verify_mode => OpenSSL::SSL::VERIFY_NONE) #**
  request = Net::HTTP::Get.new(uri.request_uri)
  response = http.request(request)
  JSON.load(response.body)
end

API_KEY  = '****************************'
url = "https://api.themoviedb.org/3/tv/2432?api_key=#{API_KEY}"
season = get_json url

season['number_of_episodes'].to_i.times do |episode|
  episode += 1 # times starts from 0 , so we add 1
  url = "https://api.themoviedb.org/3/tv/8681/season/1/episode/#{episode}?api_key=#{API_KEY}"
  episode = get_json url
  # pp episode
  puts episode['name']
end

gives

# Great White Shark: The True Story of Jaws
# Polar Bear: The Arctic Warrior
# Crocodile: The Smiling Predator
# Leopard: The Agent of Darkness
# Eagle: The Master of the Skies
# Humpback Whale: The Giant of the Oceans
# Wolf: The Legendary Outlaw
# Tiger: The Elusive Princess
# Lions: Spy in the Den
# Grizzly: Face to Face

您可以手动点击JSON端点(使用curl或Postman等)并获得您期望的结果吗?您会得到什么“随机错误”?对你来说,这可能是随机的,但错误是显著的,很少是随机的。请阅读“.Jhon Feltz,是的,我可以一个一个地获取所有url。Tin Man我的意思是,对于每次搜索,我都会得到一个错误,但通常使用未初始化的常量,因为我试图在值为零时获取多个json url。请让我知道它是否有效,然后接受答案,我还想看看最后的版本对不起,彼得先生,你的评论很好!!并更正了我的代码,但问题不是访问安全url,而是在子url依赖于第一个oneNo问题时访问多个url,并将其添加到我的答案中