json.parse doesn't like google maps suggest
Would like to use the return of: http://maps.google.de/maps/suggest?q=ham&cp=1&hl=de&gl=de&v=2&json=b as a JSON object in a firefox extension. I tried to parse it using JSON.parse and it didn't work.
Is there another way instead of using regex to use it as a JSON object?
var xhr_r开发者_StackOverflow中文版eturn = JSON.parse(rtoparse);
returns: SyntaxError: JSON.parse
other json files in my script work without problems.
The URL you're using is not part of google's official maps API and therefore subject to change, blockage, etc. . You should really use the official API. That being said, despite the URL containing json
, the result is YAML, not JSON. Set the JSON parameter to a
(instead of b
) to get a JSON result.
It is not JSON. Unlike in a JavaScript object literal, a JSON-key must be surrounded with double quotes.
An example:
The data from your link looks like this:
{suggestion:[{query:"Hamburg" ...
But it must look like this in order to be valid JSON:
{"suggestion":[{"query":"Hamburg" ...
Json from url you provided is not valid, you can check it here. So, because of json is not valid, JSON.parse
throw exception.
Fix the json format and parse it with JSON.parse
.
精彩评论