我想下面的字符串轉換與json.loads()到JSON:蟒蛇3.6未能將字符串轉換爲JSON
targetingConditions = "[{\"filters\":[{\"key\":\"domain\",\"rel\":\"neq\",\"values\":['science.howstuffworks.com', 'conduit.com', 'usatoday.com']}]}]"
我知道,反斜槓可能會導致問題,因爲我已經看到了在類似的問題和答案。我試圖替換雙反斜槓反斜槓是這樣的:
targetingConditions.replace('\\', '\\\\')
和任何其他能想到的選項或例子,我在類似的問題的答案找到。
代碼:
json.loads(targetingConditions)
輸出我得到:
Traceback (most recent call last):
File "C:/Users/uesr/Google Drive/SHARED/Automation project/Automations/streamrail/tests.py", line 36, in <module>
tag_filters = json.loads(tag_filters)
File "C:\Users\uesr\AppData\Local\Programs\Python\Python36-32\lib\json\__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "C:\Users\uesr\AppData\Local\Programs\Python\Python36-32\lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\uesr\AppData\Local\Programs\Python\Python36-32\lib\json\decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 52 (char 51)
字符串*是*(幾乎)JSON;要*解碼*它一個Python數據結構問題是單引號,這是無效的JSON – chepner
該字符串不是有效的JSON JSON字符串不能是單引號字符串是如何生成的如果硬編碼像這個例子那樣,爲什麼不只是硬編碼列表本身? –