【问题标题】:Jsonify dictionary in python in flask烧瓶中python中的Jsonify字典
【发布时间】:2021-12-26 19:03:19
【问题描述】:

我是 python 新手,我正在将烧瓶应用程序从 python2 升级到 python3,代码更改最少,但无法摆脱字典和 jsonify 的问题。

这里的“数据”是一个字典。

   message = {
                'success': True,
                'result': data
        }
        resp = jsonify(message)
        resp.status_code = 200
        return resp

收到此错误;

TypeError: 'bytes' 类型的对象不是 JSON 可序列化的

有人可以帮助解决这个问题吗?

【问题讨论】:

  • 什么是data
  • 'data' 是字典

标签: python dictionary flask jsonify


【解决方案1】:

您的数据对象类型是字节。 Json 仅适用于 unicode 字符串。解码数据应该可以解决您的问题

message = {
        'success': True,
        'result': data.decode("utf-8")
}
resp = jsonify(message)
resp.status_code = 200
return resp

【讨论】:

  • 因为 'data' 是字典,所以会出错
  • 'dict' 对象没有属性 'decode'
猜你喜欢
  • 2018-08-17
  • 1970-01-01
  • 2021-12-08
  • 1970-01-01
  • 2019-01-04
  • 2014-11-02
  • 2014-12-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多