JSON解析Google API自定义搜索错误 [英] JSON Parsing Google API Custom Search Error

查看:205
本文介绍了JSON解析Google API自定义搜索错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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)

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

And every time I run it on the google search results that come back I get the following:

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试图解析的字符串版本中的同一行:

Here is that same line in the string version which JSON is trying to parse:

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解析器似乎一直到达字符串的末尾(这是Google返回给我的一切).

Now, I've tried it with multiple search quires and I'm wondering am I doing something wrong? I added to the .to_s because the json parser tries to convert the HTTParty get statement to a string and can't and then throws an error. The JSON parser appears to get all the way to the end of the string (which is everything google has returned to me) before it throws the error.

我想念什么?

推荐答案

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

You need to add .body onto the end of the x = Search.get('http://....') like so:

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

这篇关于JSON解析Google API自定义搜索错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆