简体   繁体   English

烧瓶中python中的Jsonify字典

[英]Jsonify dictionary in python in flask

I am new to python, I am upgrading a flask app from python2 to python3 with minimal code changes, but not able to get rid of issue with dictionary and jsonify.我是python的新手,我正在将一个flask应用程序从python2升级到python3,代码更改最少,但无法摆脱字典和jsonify的问题。

Here 'data' is a dictionary.这里的“数据”是一本字典。

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

Getting this error ;得到这个错误;

TypeError: Object of type 'bytes' is not JSON serializable类型错误:“字节”类型的对象不是 JSON 可序列化的

Can someone help to get past this.?有人可以帮助解决这个问题吗?

Your data object type is bytes.您的数据对象类型是字节。 Json only works with unicode strings. Json 仅适用于 unicode 字符串。 Decoding data should solve your problem解码数据应该可以解决您的问题

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM