如何修复此意外令牌(JSON解析器错误)

时间:2016-10-12 15:05:21

标签: ruby-on-rails json ruby

我正在尝试将数据从API保存到我的数据库,但我遇到了错误。错误说明:unexpected token at 'object' (JSON::ParserError)

这是我的代码:

require 'rest-client'
require 'pp'

endpoint = "https://api.leaddyno.com/v1/leads"

class TestDyno

  def parser(page_number)
    ## API key
    request = {:params => {:key => Rails.application.secrets.LEADDYNO_PRIVATE, page: page_number}}

    ## Parse JSON
    response = JSON.parse(RestClient.get endpoint, request)

    @response_count = response.count # Count results on the page.

    pp response

    puts response_count
  end


  def data
    until @response_count == 0
      1.upto(5) do |page_number|
        response['object'].each do |item|
          LeaddynoLead.save(
              leaddyno_lead_id: item['id'],
              email: item['email'],
              first_name: item['first_name'],
              last_name: item['last_name'],
              latest_visitor_id: item['latest_visitor']['id'],
              latest_visitor_code: item['latest_visitor']['tracking_code'],
              url: item['url']['url'],
              referrer_id: item['referrer']['id'],
              referrer_url: item['referrer']['url'],
              leaddyno_affiliate_id: item['affiliate']['id'],
              leaddyno_affiliate_email: item['affiliate']['email'],
              search_term: item['search_term']['term'],
              search_engine: item['search_term']['search_engine'],
              leaddyno_tracking_code: item['tracking_code'],
              created_at: item['created_at'],
              updated_at: item['updated_at']
              )
            sleep 0.5
        end
      end
    end
  end
end

retrieve = TestDyno.new
retrieve.data

我想我需要另一组眼睛来看看这是什么问题?

如果有帮助,API文档为here。感谢。

0 个答案:

没有答案