当前位置:网站首页>成功解决: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}
边栏推荐
- Eight fallacies of distributed computing
- 2020-12-07
- What is the core of Web3?
- MySQL partition table create delete modify view
- Domestic mobile phones are snubbing low-end consumers, and Nokia provides them with high-quality products
- Yyds dry inventory MySQL learning - how transactions are isolated
- How do I make the mouse wheel work in the VB6 ide- How can I make mousewheel work in VB6 IDE?
- [string] judge whether S2 is the rotation string of S1
- Sequence list and linked list ----- advanced
- Simple database connection example
猜你喜欢

Comparaison de la taille des fractions
![[Hongmeng] use the timer to play a simple game of robbing red envelopes](/img/27/32b65dc90db7f6ece24ad39ff9b0ef.png)
[Hongmeng] use the timer to play a simple game of robbing red envelopes

绝对定位使盒子居中显示的三种方式

Introduce the functions of the new project aleo

Batch automated e-mail: Vika Vige table x Tencent Qianfan scene connector has made new moves, and business communication and event marketing only need 3 steps

2020-12-07

Mosaïque d'images basée sur la matrice de transformation
![[Business Research Report] the salary growth rate report of each industry in 2022 includes regional growth rate - download link is attached](/img/11/291f858ce2444f2fa96c89f7454c17.jpg)
[Business Research Report] the salary growth rate report of each industry in 2022 includes regional growth rate - download link is attached

(idea)the file size(2.85M) exceeds configured limit(2.56M). Code insight features are not available

Drop down menu dropdown yyds dry inventory of semantic UI
随机推荐
Mosaïque d'images basée sur la matrice de transformation
2020-12-06
Hacker + marathon =? Hacker marathon?
无限循环判断方法;
Key points of code neatness (III)
Youcans' opencv lesson - 10 Image restoration and reconstruction
vim命令大全
Cloud native overview
[DFS "want" or "don't"] seek subsets; Seeking combination
Wechat applet project example - renju for two
Go recursive infinite classification
tcp 三次握手与四次挥手
Oracle users and tablespaces
An example of how to migrate the stock equipment of the Internet of things platform to the enterprise version with one click
Summary -day11
Computer configuration suggestions for learning modeling
2020-12-17
[untitled]
[Business Research Report] analysis report on online attention of China's e-sports industry in 2021 - download link attached
Functions (arguments, formal parameters, bubbling)