【问题标题】:How to return a json response in one line in flask?如何在烧瓶中的一行中返回 json 响应?
【发布时间】:2021-12-22 12:13:41
【问题描述】:

我正在烧瓶中编写一个路由来发送这个响应,我需要在一个实线中发送这个 json 响应,但是当我发送这个 json 时(因为长度)在多行中发送

@app.route("/dashboard", methods= ['GET','POST'])
def index():
.
.
.
response = {"iDisplayStart":1,"iDisplayLength":10,
"iTotalRecords":2,"iTotalDisplayRecords":2,"data":data_ne}

return jsonify(results)

当我在下面发送这个 json 响应时返回

 {
  "iDisplayStart": 1, 
  "iDisplayLength": 10, 
  "iTotalRecords": 2, 
  "iTotalDisplayRecords": 2, 
  "data": [
  {data_ne}]}

但我想像下面这样在单行中

  {"iDisplayStart":1,"iDisplayLength":10,"iTotalRecords":2,"iTotalDisplayRecords":2,"data":data_ne}

有没有办法做到这一点?感谢您的考虑。

【问题讨论】:

  • 为什么单行很重要?
  • @balderman 我不确切知道我尝试使用我不熟悉的代码(html、jQuery、ajax),并且我知道何时以单行代码发送响应工作
  • 我认为它没有任何重要性..
  • @balderman 你是对的,我测试并且不再工作可能我更深入地研究代码并且一些不寻常的是当准确发送此响应但在''(字符串模式)下代码工作

标签: python json flask jsonify


【解决方案1】:

您是否在调试模式下运行 Flask?尝试将 JSONIFY_PRETTYPRINT_REGULAR 环境变量设置为 false。来自documentation

默认输出省略分隔符后的缩进和空格。在调试模式下或如果 JSONIFY_PRETTYPRINT_REGULAR 为 True,则输出将被格式化为更易于阅读。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-24
    • 2012-01-28
    • 2017-08-24
    • 2015-02-24
    • 1970-01-01
    • 2022-10-18
    • 2018-09-10
    相关资源
    最近更新 更多