【问题标题】:JSON Parsing Google API Custom Search ErrorJSON 解析 Google API 自定义搜索错误
【发布时间】:2011-11-17 20:38:13
【问题描述】:

我有以下代码:

require 'rubygems'
require 'httparty'
require 'pp'
require 'json'

class Search
  include HTTParty
  format :json
end


x = Search.get('https://www.googleapis.com/customsearch/v1key=AI...&cx=013...=flowers&alt=json')
x = x.to_s

result = JSON.parse(x)

每次我在返回的谷歌搜索结果上运行它时,我都会得到以下信息:

FlowerPlaces.com Delivers Fresh <b>Flowers</b> To Your Place! <br>  
Order <b>Flowers</b> Online or Call 800-411-9049 for Same Day <b>Flower</b> 
Delivery.", "cacheId"=>"v94CIDza4gQJ"}]}' (JSON::ParserError)
from /usr/local/lib/ruby/1.9.1/json/common.rb:148:in `parse'
from gg.rb:15:in `<main>'

这是 JSON 试图解析的字符串版本中的同一行:

Order <b>Flowers</b> Online or Call 800-411-9049 for Same Day <b>Flower</b> 
Delivery.\", \"cacheId\"=>\"v94CIDza4gQJ\"}]}"

现在,我已经尝试了多个搜索查询,但我想知道我做错了什么?我添加到 .to_s 是因为 json 解析器尝试将 HTTParty get 语句转换为字符串并且不能然后抛出错误。 JSON 解析器似乎在抛出错误之前一直到达字符串的末尾(这是谷歌返回给我的所有内容)。

我错过了什么?

【问题讨论】:

    标签: ruby json google-api httparty


    【解决方案1】:

    您需要将 .body 添加到 x = Search.get('http://....') 的末尾,如下所示:

    x = Search.get('http://....').body
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多