json.dumps不适用于unicode字符串的字典

时间:2017-03-21 11:31:44

标签: python json flask

我有清单:

 [{'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()

0 个答案:

没有答案