當我嘗試將此 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": "[email protected]",
"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)
我什至嘗試在在線編譯器上運行它并得到相同的結果。謝謝!
uj5u.com熱心網友回復:
問題在于反斜杠。嘗試采用原始字串:
work_orders_inprogress = r'''...
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/341438.html
