当前位置:   article > 正文

json.loads及AttributeError: ‘str‘ object has no attribute ‘decode_attributeerror: 'jsondecodeerror' object has no at

attributeerror: 'jsondecodeerror' object has no attribute 'message

json.

loads()

loads(param)是将文本字符串转换为json对象的函数,其函数名是load string 的缩写,意思是加载字符串。所以其参数param必须要是一个字典型的字符串。且字典的键必须用双引号来包裹。

在这里插入图片描述

如果是嵌套列表的话,不是str,会报错

TypeError: the JSON object must be str, bytes or bytearray, not 'dict’解决办法

dumps()

dumps(param)是将json数据对象转换为文本字符串的函数,其函数名是dump string 的缩写,意思是输出字符串,所以其参数param必须要是json对象,也就是loads()函数返回的数据类型。

两个语法
Encode: 把普通字符串 转为 机器可识别的bytes
Decode: 把bytes转为字符串

在这里插入图片描述

在这里插入图片描述

两个差异
Python3的str 默认不是bytes,所以不能decode,只能先encode转为bytes,再decode
python2的str 默认是bytes,所以能decode

所以str.decode 本质是bytes类型的str的decode
python3经常出现 AttributeError: ‘str’ object has no attribute ‘decode’

非要这样玩,只能先encode转为bytes,再decode
强制转换忽略错误:
bytes.decode(‘’utf-8‘’, ‘’ignore‘’)

记忆小技巧
编码就是encode,把你认识的转为,机器人认识的
解码decode,就是吧一堆机器认识的,解释为人能读懂的

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/759160
推荐阅读
相关标签
  

闽ICP备14008679号