我已使用flask
启动了python
个应用。我使用yelp-python
https://github.com/Yelp/yelp-python访问yelp api
from yelp.client import Client
from yelp.oauth1_authenticator import Oauth1Authenticator
from IPython import embed
auth = Oauth1Authenticator(
consumer_key='key',
consumer_secret='secret',
token='token',
token_secret='secret'
)
client = Client(auth)
app = Flask(__name__)
@app.route('/hello')
def hello_world():
response = client.search('pizza hut')
embed()
return jsonify(response)
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0')
响应为<yelp.obj.search_response.SearchResponse object at 0x7fab7fc80c50>
,jsonify
表示无法对其进行序列化。
如何序列化它以便我可以发回json
?
我可以解析response.businesses
给我一个list
,但即使jsonify似乎也不接受。
以下是一些手动解析的示例:
In [1]: response
Out[1]: <yelp.obj.search_response.SearchResponse at 0x7faa0806d940>
In [2]: response.businesses
Out[2]:
[<yelp.obj.business.Business at 0x7faa0809a1d0>,
<yelp.obj.business.Business at 0x7faa0809a208>,
<yelp.obj.business.Business at 0x7faa0809a278>,
<yelp.obj.business.Business at 0x7faa0809a320>,
<yelp.obj.business.Business at 0x7faa0809a3c8>,
<yelp.obj.business.Business at 0x7faa0809a470>,
<yelp.obj.business.Business at 0x7faa0809a518>,
<yelp.obj.business.Business at 0x7faa0809a5c0>,
<yelp.obj.business.Business at 0x7faa0809a668>,
<yelp.obj.business.Business at 0x7faa0809a710>,
<yelp.obj.business.Business at 0x7faa0809a7b8>,
<yelp.obj.business.Business at 0x7faa0809a860>,
<yelp.obj.business.Business at 0x7faa0809a908>,
<yelp.obj.business.Business at 0x7faa0809a9b0>,
<yelp.obj.business.Business at 0x7faa0809aa58>,
<yelp.obj.business.Business at 0x7faa0809ab00>,
<yelp.obj.business.Business at 0x7faa0809aba8>,
<yelp.obj.business.Business at 0x7faa0809ac50>,
<yelp.obj.business.Business at 0x7faa0809acf8>,
<yelp.obj.business.Business at 0x7faa0809ada0>]
In [3]: response.businesses[0]
Out[3]: <yelp.obj.business.Business at 0x7faa0809a1d0>
In [4]: response.businesses[0].name
Out[4]: 'Franco Manca'
我想我可以手动设置list
dictionaries
。我想如果我可以解析&lt;对象在&gt;进入一个json对象。如果有人能解释< object at >
以及如何处理它,我会很感激。感谢
答案 0 :(得分:1)
我很确定“at”之后的所有内容都是记忆中的位置。
那就是说,它只意味着你有一个python对象。您可以使用dir
检查python对象上的可用属性,以查看它具有的属性。但是,以下应该主要做你想要的事情:
map(lambda x : x.__dict__, response.businesses)