从嵌套json

时间:2015-08-23 13:51:18

标签: ruby json

我正在尝试从嵌套的json中获取数据。在这里无法理解这个问题。请忽略我传递给ChildArticle类的字段。我可以解决这个问题。

JSON的网址 - http://api.nytimes.com/svc/mostpopular/v2/mostshared/all-sections/email/30.json?api-key=31fa4521f6572a0c05ad6822ae109b72:2:72729901

以下是我的代码:

url = 'http://api.nytimes.com'

#Define the HTTP object
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)

#If the api being scraped uses https, then set use_ssl to true.
http.use_ssl = false

#Define the request_url
#Make a GET request to the given url
request = '/svc/mostpopular/v2/mostshared/all-sections/email/30.json?api-key=31fa4521f6572a0c05ad6822ae109b72:2:72729901'
response = http.send_request('GET', request)

#Parse the response body
forecast = JSON.parse(response.body)
forecast["results"]["result"].each do |item|

    date = Date.parse(item["published_date"].to_s)
    if (@start <= date) && (@end >= date)
        article = News::ChildArticle.new(author: item["author"], title: item["title"], summary: item["abstract"],
                                         images: item["images"],source: item["url"], date: item["published_date"],
                                         guid: item["guid"], link: item["link"], section: item["section"], 
                                         item_type: item["item_type"], updated_date: item["updated_date"], 
                                         created_date: item["created_date"], 
                                         material_type_facet: item["material_type_facet"])

        @articles.concat([article])
    end
end

我得到以下错误 -

  

[]': no implicit conversion of String into Integer (TypeError) at预测[“结果”] [“结果”]。每个do | item |`

0 个答案:

没有答案