Python 官方文档:入门教程 => 点击学习
目录问题描述问题分析问题解决1、将原格式换成双引号2、换其它方式来转换总结问题描述 C:\Users\lenovo\AppData\Local\Programs\python\pyt
C:\Users\lenovo\AppData\Local\Programs\python\python37\Python.exe D:/360MoveData/Users/lenovo/Desktop/startProject/OAS.Cloud.PAAS_Interface/TestCases/UM/Test_01_reGISter.py
D:\360MoveData\Users\lenovo\Desktop\startProject\OAS.Cloud.PAAS_Interface\common\CaseData.ini
Traceback (most recent call last):
File "D:/360MoveData/Users/lenovo/Desktop/startProject/OAS.Cloud.PAAS_Interface/TestCases/UM/Test_01_register.py", line 73, in <module>
Test_UM_register().test_01()
File "D:/360MoveData/Users/lenovo/Desktop/startProject/OAS.Cloud.PAAS_Interface/TestCases/UM/Test_01_register.py", line 52, in test_01
headers = JSON.loads(dataList[1])
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\lib\json\decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)Process finished with exit code 1
如上,json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes
意思为jsondecodeerror:期望属性名用双引号括起来
我查看我的原始数据,格式为:
{‘Content-Type': ‘application/json'}
python对单引号、双引号都认为是引号,但对于json来说,它识别是按照双引号来的。因此,凡是用json来格式转换,必定将单引号转为双引号,必要的时候加“\”转义。
通过上述问题分析,我们得到两个解决方案:
{‘Content-Type': ‘application/json'}
改为
{“Content-Type”: “application/json”}
验证结果:
成功
headers = eval(dataList[1])
# headers = json.loads(dataList[1])
验证结果:
成功
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: python使用json将字符串转字典报错的解决
本文链接: https://lsjlt.com/news/195157.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0