将Google结果对象(纯js)转换为Python对象 [英] Convert Google results object (pure js) to Python object

查看:66
本文介绍了将Google结果对象(纯js)转换为Python对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在尝试使用Google Map建议API来请求地名建议。不幸的是我找不到这个位的文档。

So I'm trying to use Google Map suggest API to request place name suggestions. Unfortunately I can't find the docs for this bit.

这是一个示例URI:

http://maps.google.com/maps/suggest?q=lon&cp=3&ll=55.0,-3.5&spn=11.9,1.2&hl=en& gl = uk& v = 2

返回:

{suggestion:[{query:"London",...

我想使用这在python(2.5)中。现在使用正确的JSON,键盘周围会有如下引用:

I want to use this in python (2.5). Now in proper JSON there would have been quotations around the keys like so:

{"suggestion":[{"query":"London",...

我可以使用simplejson或其他东西,但因为它是我有点卡住。

and I could have used simplejson or something, but as it is I'm a bit stuck.

这里有两种可能的解决方案;或者我可以获得API代码并找到返回正确JSON的选项,或者我在python中执行此操作。

There are two possible solutions here; either I can get to the API code and find an option to return proper JSON, or I do that in python.

请提出任何想法。

推荐答案

呃,这确实很烦人。这是一个JavaScript文字,但它 - 毫无意义 - 不是JSON。

Ugh, that's indeed pretty annoying. It's a JavaScript literal but it — pointlessly — isn't JSON.

理论上你应该能够导入 json.decoder.JSONDecoder 来自Python stdlib(或 simplejson pre-2.6,它是相同的)并将其子类化,然后将该子类传递给 json.loads 覆盖解码器行为。实际上这并不是真的可行,因为 json.decoder 充满了抵制子类化的全局交叉引用,你需要改变的位是slap bang in the middle of def JSONObject

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.

因此,可能值得查看其他Python JSON库。我发现这个,在'非严格'模式下,将解析不带引号的对象属性姓名:

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': ...

这篇关于将Google结果对象(纯js)转换为Python对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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