JSON Google API Custom Search Error Analysis

I have the following code:

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) 

And every time I run it in google search results that return, 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>' 

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

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

Now I tried this with a few search queries, and I am wondering if I am doing something wrong? I added to .to_s because the json parser tries to convert the HTTParty get statement to a string and cannot and then throws an error. The JSON parser seems to get to the end of the line (that's all google returned to me) before it throws an error.

What am I missing?

+1
source share
1 answer

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

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

Source: https://habr.com/ru/post/888213/


All Articles