简体   繁体   中英

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.

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

Can someone help to get past this.?

Your data object type is bytes. Json only works with unicode strings. Decoding data should solve your problem

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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