我有清单:
[{'probabilities': {'wrong': 0.99991131, 'correct': 8.873138e-05}, 'sentence': u'hello world'}, {'probabilities': {'wrong': 0.99996436, 'correct': 3.5610035e-05}, 'sentence': u'London is the capital of Great Britain'}]
json.dumps不会将此列表转换为字符串,甚至不会抛出任何异常
有完整的代码:
import json
from flask import Flask
from flask import request
from network import predict
app = Flask(__name__)
@app.route("/", methods=['POST'])
def main():
res = []
for obj in request.json:
res.append({'sentence': obj['sentence'], 'probabilities': predict(obj['sentence'])})
print('res', res)
try:
print('json.dumps(res)', json.dumps(res, ensure_ascii=False))
except Exception:
print('Exception', Exception)
return json.dumps(res)
if __name__ == "__main__":
app.run()