from flask import Flask, abort, request, jsonify
app = Flask(name)
tasks = []
@app.route('/add_task/', methods=['POST']) def add_task(): if not request.json or 'id' not in request.json or 'info' not in request.json: abort(400) task = { 'id': request.json['id'], 'info': request.json['info'] } tasks.append(task) return jsonify({'result': 'success'})
@app.route('/get_task/', methods=['GET']) def get_task(): if not request.args or 'id' not in request.args: # 没有指定 id 则返回全部 return jsonify(tasks) else: task_id = request.args['id'] task = filter(lambda t: t['id'] == int(task_id), tasks) return jsonify(task) if task else jsonify({'result': 'not found'})
if name == "main": app.run(debug=True)
当 tasks 是[{"id":123, "info": "测试"},{"id":456, "info":"测试 2"}] 我 get 请求是 http://127.0.0.1:5000/get_task/?id=123 为什么报错 TypeError: Object of type 'filter' is not JSON serializable
1
yxcxx 2018-07-26 17:46:39 +08:00
task = filter(lambda t: t['id'] == int(task_id), tasks)
这里 task 是一个 filter,并不是一个可以序列化的 列表,字典 之类的 |