json.decoder.JSONDecodeError: 解决方法

通过ast模块处理
Source Code:

Python
<br />import ast str_info = '{"name": "nock", "age": 18}' dict_info = ast.literal_eval(str_info) print("string info type is -->: %s" % (type(str_info))) print("dict info type is -->: %s" % (type(dict_info))) s_info = "{'name': 'nock', 'age': 18}" d_info = ast.literal_eval(s_info) print("s info type is -->: %s" % (type(s_info))) print("d info type is -->: %s" % (type(d_info)))
1
2
3
4
5
6
7
8
9
10
11
12
13
< br / > import ast
str_info = '{"name": "nock", "age": 18}'
dict_info = ast . literal_eval ( str_info )
 
print ( "string info type is -->: %s" % ( type ( str_info ) ) )
print ( "dict info type is -->: %s" % ( type ( dict_info ) ) )
 
s_info = "{'name': 'nock', 'age': 18}"
d_info = ast . literal_eval ( s_info )
 
print ( "s info type is -->: %s" % ( type ( s_info ) ) )
print ( "d info type is -->: %s" % ( type ( d_info ) ) )
 

Result:

Python
string info type is -->: <class 'str'> dict info type is -->: <class 'dict'> s info type is -->: <class 'str'> d info type is -->: <class 'dict'>
1
2
3
4
5
string info type is -- > : < class 'str' >
dict info type is -- > : < class 'dict' >
s info type is -- > : < class 'str' >
d info type is -- > : < class 'dict' >
 

使用ast.literal_eval进行转换既不存在使用json 模块进行转换的问题,也不存在使用eval模块进行转换的安全性问题,因此推荐大家使用ast.literal_eval的方法。




  • zeropython 微信公众号 5868037 QQ号 5868037@qq.com QQ邮箱
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]中提到的报错"json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 267 (char 266)"通常是由于JSON格式错误导致的。JSON格式要求在每个键值对之间使用逗号进行分隔,但在给定的位置上缺少了逗号。这可能是由于数据截取不正确或者字符串转换为字典时出现问题引起的。引用\[2\]中提到了解决这个问题的方法,可以使用eval方法将字符串的字典转换为字典,然后使用json.dumps方法将其转换为JSON格式的数据,最后使用json.loads方法进行解析。这样就可以避免JSON解析错误。引用\[3\]中也提到了数据截取不正确导致JSON格式错误的情况。因此,你可以检查你的代码中的数据截取部分,确保截取的数据完整且符合JSON格式的要求。 #### 引用[.reference_title] - *1* *3* [出现“json.decoder.JSONDecodeError: Expecting ‘,‘ delimiter: line 1 column 267 (char 266)”报错的...](https://blog.csdn.net/weixin_56198196/article/details/120409868)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [生产环境json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line x ...](https://blog.csdn.net/qq_43224174/article/details/127671272)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值