重新生成JSON列表

时间:2016-03-27 20:37:22

标签: python json

在路径/getAll中我想返回一个JSON列表,但我得到列表对象不可调用
为什么会发生这种情况以及如何返回我的Json列表?以前 myList list ,在阅读完定义后,我将“list”更改为“myList”,但错误仍然存​​在。

  

TypeError:'str'对象不可调用

     

TypeError:'list'对象不可调用

     

尝试使用这些名称为变量赋值或通过改变它们的值时,可能会发生这种情况。

代码

from flask import Flask, render_template, Response
import pymongo
from bson.json_util import dumps
app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/getAll', methods=['GET'])
def getAll():
    try:
        conn = pymongo.MongoClient()
    except pymongo.errors.ConnectionFailure, e:
        print "Could not connect to MongoDB: %s" % e
    db = conn['local']
    collection = db.test
    myList = []
    for d in collection.find():
        myList.append(dumps(d))
    return myList


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8080, debug=True)

TrackTrace

127.0.0.1 - - [27/Mar/2016 16:51:44] "GET /getAll HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1478, in full_dispatch_request
    response = self.make_response(rv)
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1577, in make_response
    rv = self.response_class.force_type(rv, request.environ)
  File "/usr/lib/python2.7/site-packages/werkzeug/wrappers.py", line 847, in force_type
    response = BaseResponse(*_run_wsgi_app(response, environ))
  File "/usr/lib/python2.7/site-packages/werkzeug/wrappers.py", line 57, in _run_wsgi_app
    return _run_wsgi_app(*args)
  File "/usr/lib/python2.7/site-packages/werkzeug/test.py", line 871, in run_wsgi_app
    app_rv = app(environ, start_response)
TypeError: 'list' object is not callable

0 个答案:

没有答案