当我试图将这个JSON字符串转换为Python字典时,我一直得到无效的转义错误。下面是代码和回溯。这是用于发送比这更多信息的API,但这是我在保持相同错误的同时减少的。我在这个字符串中使用了\和,得到了相同的结果。
import json
work_orders_inprogress = '''
{
"ItemCollection": [
{
"Header": {
"ID": "work_orderID",
"CreationTime": "2021-03-17T12:12:47.02-04:00",
"DeletionTime": "0001-01-01T00:00:00",
"DeletionTimeSpecified": true,
"LastModifiedTime": "2021-10-29T12:12:23.73-04:00",
"LastModifiedBy": "Mit_rando.Province\First Last"
},
"ID": "work_orderID",
"Type": "WorkOrder",
"ScheduledTime": "2021-03-19T12:06:00.937-04:00",
"PromisedTime": "2021-06-01T19:00:00-04:00",
"InvoiceTime": "0001-01-01T00:00:00",
"WorkOrderNumber": 35659,
"InvoiceNumber": 0,
"PurchaseOrderNumber": "",
"Contact": {
"Header": {
"ID": "work_orderID",
"CreationTime": "2021-03-09T13:00:06.77-05:00",
"DeletionTime": "0001-01-01T00:00:00",
"DeletionTimeSpecified": true,
"LastModifiedTime": "2021-08-30T15:21:28.25-04:00",
"LastModifiedBy": "Mit_rando.province\First Last"
},
"ID": "work_orderID",
"FileAs": "Business - CASH CUSTOMER",
"Name": {
"Title": "Manager",
"Prefix": "",
"FirstName": "First",
"MiddleName": "",
"LastName": "Last",
"Suffix": ""
},
"Address": {
"Title": "Business",
"ID": "work_orderID",
"Street": "some random st",
"City": "cityname",
"Province": "XX",
"PostalCode": "XXXXX",
"Country": "USA"
},
"Company": "Company Name - CASH CUSTOMER",
"Phone1Title": "Business",
"Phone1": "(720) fakenumber",
"Phone2Title": "Cell",
"Phone2": "(303) fakenumber",
"EmailTitle": "Email",
"Email": "fake@email.org",
"PreferredContactMethod": "Email",
"MarketingSource": "",
"Note": "",
"NoMessaging": false,
"NoEmail": false,
"NoPostCard": false
}
}
]
}
'''
data = json.loads(work_orders_inprogress)
print(type(data))
错误:
Traceback (most recent call last):
File "<string>", line 70, in <module>
File "/usr/lib/python3.8/json/__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.8/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.8/json/decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Invalid escape: line 11 column 54 (char 386)
我甚至试过在在线编译器上运行这个,并得到相同的结果。谢谢!
问题出在反斜杠上。尝试使用原始字符串:
work_orders_inprogress = r'''...