Convert Google Results Object (pure Js) To Python Object
Solution 1:
Ugh, that's indeed pretty annoying. It's a JavaScript literal but it — pointlessly — isn't JSON.
In theory you are supposed to be able to import json.decoder.JSONDecoder
from the Python stdlib (or simplejson
pre-2.6, which is the same) and subclass it, then pass that subclass to json.loads
to override decoder behaviour. In reality this isn't really feasible as json.decoder
is full of global cross-references that resist subclassing, and the bit you need to change is slap bang in the middle of def JSONObject
.
So it's probably worth looking at other Python JSON libraries. I found this one which, in ‘non-strict’ mode, will parse unquoted object property names:
>>> import demjson
>>> demjson.decode('{suggestion:[{query:"London",interpretation: ...')
{u'suggestion': [{u'query': u'London', u'operation': 2, u'interpretation': ...
Solution 2:
I would try to poke around in order to get JSON, but failing that there's this little monstrosity which someone will inevitably yell at me about:
classIden(object):
def __getitem__(name, index):
return index
notjson = '{...}'data = eval(notjson, {}, Iden())
Solution 3:
import demjson
demjson.decode(google.js)
I found this when trying to parse Google Finance option "JSON" data, which, as many note, isn't JSON-compliant.
demjson saved me writing an obnoxious regex string; it just works.
Post a Comment for "Convert Google Results Object (pure Js) To Python Object"