2015-09-04 80 views
0

我使用python庫python-twitter來訪問twitter api。通過twitter搜索json響應輸出json的瓶子路由

我正在做一個搜索查詢,並希望響應去我的燒瓶路線。

我得到這個埃羅

TypeError: <twitter.status.Status object at 0x7f16dc089110> is not JSON serializable 

這裏是我有什麼

@app.route('/t/search/tweets', methods=['GET']) 
def twit(): 
    results = twitter_api.GetSearch(count=count, term='hello world') 
    return Response(json.dumps(results), mimetype='application/json') 

這裏是整個引用

Traceback (most recent call last): 
    File "/home/john/.virtualenvs/in5/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__ 
    return self.wsgi_app(environ, start_response) 
    File "/home/john/.virtualenvs/in5/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app 
    response = self.make_response(self.handle_exception(e)) 
    File "/home/john/.virtualenvs/in5/lib/python2.7/site-packages/flask_cors/extension.py", line 110, in wrapped_function 
    return cors_after_request(app.make_response(f(*args, **kwargs))) 
    File "/home/john/.virtualenvs/in5/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception 
    reraise(exc_type, exc_value, tb) 
    File "/home/john/.virtualenvs/in5/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app 
    response = self.full_dispatch_request() 
    File "/home/john/.virtualenvs/in5/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request 
    rv = self.handle_user_exception(e) 
    File "/home/john/.virtualenvs/in5/lib/python2.7/site-packages/flask_cors/extension.py", line 110, in wrapped_function 
    return cors_after_request(app.make_response(f(*args, **kwargs))) 
    File "/home/john/.virtualenvs/in5/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception 
    reraise(exc_type, exc_value, tb) 
    File "/home/john/.virtualenvs/in5/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request 
    rv = self.dispatch_request() 
    File "/home/john/.virtualenvs/in5/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request 
    return self.view_functions[rule.endpoint](**req.view_args) 
    File "/home/john/Scripts/Python/in5/views.py", line 73, in twit 
    return Response(json.dumps(results), mimetype='application/json') 
    File "/usr/lib/python2.7/json/__init__.py", line 243, in dumps 
    return _default_encoder.encode(obj) 
    File "/usr/lib/python2.7/json/encoder.py", line 207, in encode 
    chunks = self.iterencode(o, _one_shot=True) 
    File "/usr/lib/python2.7/json/encoder.py", line 270, in iterencode 
    return _iterencode(o, 0) 
    File "/usr/lib/python2.7/json/encoder.py", line 184, in default 
    raise TypeError(repr(o) + " is not JSON serializable") 
TypeError: <twitter.status.Status object at 0x7fc3d3384390> is not JSON serializable 

這裏是 '打印結果'

[<twitter.status.Status object at 0x7ff1f3114f90>, <twitter.status.Status object at 0x7ff1f30c6050>, <twitter.status.Status object at 0x7ff1f30c6190>, <twitter.status.Status object at 0x7ff1f30c6310>, <twitter.status.Status object at 0x7ff1f30c6510>, <twitter.status.Status object at 0x7ff1f30c65d0>, <twitter.status.Status object at 0x7ff1f30c6690>, <twitter.status.Status object at 0x7ff1f30c67d0>, <twitter.status.Status object at 0x7ff1f30c69d0>, <twitter.status.Status object at 0x7ff1f30c6c10>, <twitter.status.Status object at 0x7ff1f30c6d90>, <twitter.status.Status object at 0x7ff1f30c6e50>, <twitter.status.Status object at 0x7ff1f30c90d0>, <twitter.status.Status object at 0x7ff1f30c91d0>, <twitter.status.Status object at 0x7ff1f30c9290>, <twitter.status.Status object at 0x7ff1f30c9410>, <twitter.status.Status object at 0x7ff1f30c95d0>, <twitter.status.Status object at 0x7ff1f30c9690>, <twitter.status.Status object at 0x7ff1f30c9750>, <twitter.status.Status object at 0x7ff1f30c9a10>, <twitter.status.Status object at 0x7ff1f30c9ad0>, <twitter.status.Status object at 0x7ff1f30c9c10>, <twitter.status.Status object at 0x7ff1f30c9dd0>, <twitter.status.Status object at 0x7ff1f30c9e90>, <twitter.status.Status object at 0x7ff1f30c9f90>, <twitter.status.Status object at 0x7ff1f30cf1d0>, <twitter.status.Status object at 0x7ff1f30cf290>, <twitter.status.Status object at 0x7ff1f30cf510>, <twitter.status.Status object at 0x7ff1f30cf5d0>, <twitter.status.Status object at 0x7ff1f30cf690>, <twitter.status.Status object at 0x7ff1f30cf810>, <twitter.status.Status object at 0x7ff1f30cf8d0>, <twitter.status.Status object at 0x7ff1f30cfa50>, <twitter.status.Status object at 0x7ff1f30cfc90>, <twitter.status.Status object at 0x7ff1f30cfed0>, <twitter.status.Status object at 0x7ff1f30cff90>, <twitter.status.Status object at 0x7ff1f30d6090>, <twitter.status.Status object at 0x7ff1f30d6210>, <twitter.status.Status object at 0x7ff1f30d6390>, <twitter.status.Status object at 0x7ff1f30d6590>, <twitter.status.Status object at 0x7ff1f30d66d0>, <twitter.status.Status object at 0x7ff1f30d6890>, <twitter.status.Status object at 0x7ff1f30d6990>, <twitter.status.Status object at 0x7ff1f30d6a10>, <twitter.status.Status object at 0x7ff1f30d6a90>, <twitter.status.Status object at 0x7ff1f30d6bd0>, <twitter.status.Status object at 0x7ff1f30d6c90>, <twitter.status.Status object at 0x7ff1f30d6e50>, <twitter.status.Status object at 0x7ff1f30dc050>, <twitter.status.Status object at 0x7ff1f30dc110>] 
+0

結果如何?向我們顯示數據 – taesu

+0

@taesu將其添加到 – nadermx

+0

我的意思是結果變量。 – taesu

回答

2

我只好掏自己的源代碼,它原來必須使用AsDict()

json_results = [result.AsDict() for result in results] 

這樣的痛苦......

我使用tweepy而不是此lib中牢牢推薦缺少文件。

+0

但在[dev文檔](https://dev.twitter.com/rest/reference/get/search/tweets)它說應用程序json – nadermx

+0

你可以粘貼什麼'結果'看起來像? – taesu

+0

TypeError:不是JSON可串行化 – nadermx

0

很明顯,python-twitter已經將api調用結果反序列化爲一個自定義的twitter.status.Status對象(或其列表)。默認情況下,json.dump()知道只有基本類型(字符串,數字,列表和類型的字典),所以你必須提供其他的事你自己的編碼器,如這裏說明:https://docs.python.org/2/library/json.html#encoders-and-decoders

您可能還需要閱讀此:How to make a class JSON serializable

+0

你可以使用'instance.AsDict()'。顯然它沒有寫在他們的文檔中的任何地方。 (我甚至不認爲他們有自己的文檔...) – taesu