当前位置:网站首页>成功解决:TypeError: the JSON object must be str, bytes or bytearray, not dict
成功解决:TypeError: the JSON object must be str, bytes or bytearray, not dict
2022-06-12 03:36:00 【小袁ITSuper】
成功解决:TypeError: the JSON object must be str, bytes or bytearray, not dict
前言
本文已收录于:《告别Bug》专栏
本专栏用于记录学习和工作中遇到的各种疑难Bug问题及解决思路+解决方法,包括程序安装、运行程序过程中的问题等等,订阅专栏后如遇到其他问题可私聊帮忙解决!!!
报错问题
我的代码:
import json
data = {
"name": '小白', "age": 20}
print(data, type(data))
dic_str = json.loads(data)
print(dic_str)
报错内容:
Traceback (most recent call last):
File "E:/Python学习/2.py", line 5, in <module>
dic_str = json.loads(data)
File "F:\Python3.8\lib\json\__init__.py", line 341, in loads
raise TypeError(f'the JSON object must be str, bytes or bytearray, '
{
'name': '小白', 'age': 20} <class 'dict'>
TypeError: the JSON object must be str, bytes or bytearray, not dict
报错原因
报错内容翻译:TypeError:JSON对象必须是str、bytes或bytearray,而不是dict
报错原因:单引号的字符串不符合Json的标准格式
解决方法
解决方法:现在是一个字典,用str将它转换成字符串,使用replace把单引号换成双引号
代码修改:
import json
data = {
"name": '小白', "age": 20}
print(data, type(data))
dic_str = json.loads(str(data).replace("'", "\""))
print(dic_str)
再次运行:
{
'name': '小白', 'age': 20} <class 'dict'>
{
'name': '小白', 'age': 20}
边栏推荐
- Inverted string - two solutions
- Unity脚本出現missing時的解决方法
- CA證書及密鑰對應用筆記
- 顺序表与链表-----进阶
- Sed command
- Go recursive infinite classification
- AI interview bag | Netease mutual entertainment AI Lab artificial intelligence research engineers share on both sides
- [Yugong series] March 2022 asp Net core Middleware - conditional routing
- Addition and multiplication of large integers;
- [data recovery in North Asia] data recovery in which the logical volume of the server is changed and the file system is damaged due to system reinstallation
猜你喜欢

MongoDB精华总结

根據變換矩陣進行圖像拼接

Image mosaic based on transformation matrix
![Leetcode 6[finding rules] Z-transform the leetcode path of heroding](/img/c0/aca2eb185ce4b423df9ee171ee91e1.jpg)
Leetcode 6[finding rules] Z-transform the leetcode path of heroding

Special information | liquor (Baijiu, beer, wine)

Special materials | household appliances, white electricity, kitchen electricity

Wechat applet project example - renju for two

Exemple de projet d'applet Wechat - calculatrice de constitution

Convert py file to EXE file

【mysql】mysql安装
随机推荐
Solution en cas de défaillance du script Unity
Yyds dry inventory MySQL learning - how transactions are isolated
2020-12-17
[mysql][mysql 8.0 compressed package installation method]
根據變換矩陣進行圖像拼接
Message queuing overview
2020-12-17
string manipulation:
Go 语法 变量
Wechat applet project example - I have a paintbrush (painting)
The road of global evolution of vivo global mall -- multilingual solution
Sequence list and linked list ----- advanced
[C language] dynamic memory allocation
What is a request response pair called? [repeat] - what is a request response pair called? [duplicate]
The idea of setting the flash memory management and resource size, and the quantitative relationship among parallelism, slot, and taskmanager quantity
Redis gets the set of keys prefixed with XXX
PostgreSQL basic introduction and deployment
xml
Experience the good team collaborative development process brought by Huawei cloud code hosting from 0 [my Huawei cloud experience journey] [play with Huawei cloud]
简单的数据库连接示例