0

我试图从他们的 API 中提取 NY Times Bestseller 信息,然后用 Ruby 解析和打印出所有漂亮的东西。

由于某种原因,它不起作用。可能是因为我是个白痴?请善待。我是一个完全的初学者。

runner.rb
require 'json'
require 'rest-client'
require_relative 'books.rb'

result = RestClient.get('http://api.nytimes.com/svc/books/v2/lists/Combined-Print-and-E-Book-Fiction.json?&offset=&sortby=&sortorder=&api-key=**********************')
parsed = JSON.parse result

books = parsed["results"].each do |results_hash|
 results_hash["book_details"].collect do |s|
  Bestseller.new s["title"], s["description"], s["author"]
 end
end

puts "Here's some books"
books.each do |infos|
 puts infos
end

books.rb
class Bestseller
  def initialize title, description, author
    @title = title
    @description = description
    @author = author
  end
end

好的,所以在下面提出建议。错误消失了。但是现在 books 变量正在显示我不想要的东西。我只想要标题、描述、作者。

[{"list_name"=>"Combined Print and E-Book Fiction", "display_name"=>"Combined Print & E-Book Fiction", "updated"=>"WEEKLY", "bestsellers_date"=>"2013-11-02", "published_date"=>"2013-11-17", "list_image"=>"9781101626368.jpg", "normal_list_ends_at"=>15, "rank"=>1, "rank_last_week"=>0, "weeks_on_list"=>1, "asterisk"=>0, "dagger"=>0, "isbns"=>[{"isbn10"=>"0425259854", "isbn13"=>"9780425259856"}], "book_details"=>[{"title"=>"DARK WITCH", "description"=>"In the first book of the Cousins O'Dwyer trilogy, Iona Sheehan moves to Ireland to investigate her family's history.", "contributor"=>"by Nora Roberts", "author"=>"Nora Roberts"

如果您想/需要查看原始 json 数据,请访问:http: //api.nytimes.com/svc/books/v2/lists/Combined-Print-and-E-Book-Fiction.json? &offset= &sortby=&sortorder=&api-key=6504A37DEA8BB8DC7B13807A5DA44D16:17:68383866

添加哈希示例:

{"status"=>"OK", 
"copyright"=>"Copyright (c) 2013 The New York Times Company.  All Rights Reserved.", 
"num_results"=>25, 
"last_modified"=>"2013-11-08T13:15:36-05:00", 
"results"=>
    [
      {"list_name"=>"Combined Print and E-Book Fiction", 
      "display_name"=>"Combined Print & E-Book Fiction", 
      "updated"=>"WEEKLY", 
      "bestsellers_date"=>"2013-11-02", 
      "published_date"=>"2013-11-17", 
      "list_image"=>"9781101626368.jpg", 
      "normal_list_ends_at"=>15, "
      rank"=>1, 
      "rank_last_week"=>0, 
      "weeks_on_list"=>1, 
      "asterisk"=>0, 
      "dagger"=>0, 
      "isbns"=>
        [
          {"isbn10"=>"0425259854", 
          "isbn13"=>"9780425259856"}
        ], 
      "book_details"=> 
        [
          {"title"=>"DARK WITCH", 
          "description"=>"In the first book of the Cousins O'Dwyer trilogy, Iona Sheehan moves to Ireland to investigate her family's history.", 
          "contributor"=>"by Nora Roberts", 
          "author"=>"Nora Roberts",
4

1 回答 1

1

看起来您没有正确解析结果。 results是一个数组,其中包含一个键为 的散列book_details。所以,你会想要这样的东西:

books = parsed["results"].each do |results_hash|
  results_hash["book_details"].map do |book_details_hash|
    book = Bestseller.new book_details_hash["title"], book_details_hash["description"], book_details_hash["author"]

    book.save!
  end
end

上面的添加将 设置Bestseller为变量 book 然后保存它。另一种方法是使用Bestseller.create而不是Bestseller.new.

于 2013-11-08T21:55:34.937 回答