Flask jsonify - 如何发回字符串?

时间:2016-07-10 11:06:52

标签: python json utf-8 flask

我构建了以字符串格式发送结果的API。

    <div id="a1">
      <p> The title is normal, but when you hover down... </p>
    </div>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>

    <div id="a2" class="target label label-info 1" title="Article 2 | Prospekt">
      <p style="padding:5px"> Stackoverflow is an amazing place! Please check your title if someting happened! *something happens magically* </p>
    </div>

    <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>

        <div id="a1" class="target label label-info" title="Article 3 | Prospekt">
          <p style="padding:5px"> This is the 3rd article, the title should change lol </p>
        </div>

        <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>

            <div id="a2" class="target label label-info" title="Article 4 | Prospekt">
              <p style="padding:5px"> This is the 4th Article, Something on the title should happen or it should change </p>
            </div>

            <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>

                <div id="a1" class="target label label-info" title="Article 5 | Prospekt">
                  <p style="padding:5px"> Prospekt | A Gaming Community <br> &copy Kirk Niverba and Stackoverflow for helping me to finish the scrollTitleJS! </p>
                </div>
<br>

尝试@app.route('/', methods=['POST']) def converter(): content = request.json # translate() - english to hindi converted = translate(content) print converted #prints string normally return jsonify({"result":converted}) 时,它会将我发回给我

curl -H "Content-Type: application/json" -X POST -d '{"text":"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor."}' http://localhost:5000/

如何发回字符串而不是json对象?

2 个答案:

答案 0 :(得分:2)

不要将结果包装在jsonify来电中,只需return converted

@app.route('/', methods=['POST'])
def converter():
    content = request.json
    converted = translate(content)
    return converted

答案 1 :(得分:1)

我认为jsonify默认使用ASCII。有一个配置选项:

app.config['JSON_AS_ASCII'] = False

将默认值设置为unicode。