我想下面的字符串转换与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字符串不能是单引号字符串是如何生成的如果硬编码像这个例子那样,为什么不只是硬编码列表本身? –