如何将此 json 字符串转换为字典?
How to convert this json string to dict?
执行以下代码后:
import json
a = '{"excludeTypes":"*.exe;~\$*.*"}'
json.loads(a)
我得到:
Traceback (most recent call last):
File "", line 1, in
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/init.py", line 338, in loads
return _default_decoder.decode(s)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 365, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 381, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Expecting property name: line 1 column 2 (char 1)
那么如何将 'a' 转换为字典。
请注意,字符串已经在 'a' 中,我不能在它前面添加 'r'。理想情况下,字符串应该是 {"excludeTypes":"*.exe;~\\$*.*"}
此外,以下代码不起作用:
import json
a = '{"excludeTypes":"*.exe;~\$*.*"}'
b = repr(a)
json.loads(b)
import ast
d = ast.literal_eval(a)
通过转义转义字符“\”:
import json
a = '{"excludeTypes":"*.exe;~\$*.*"}'
a = a.replace("\","\\")
json.loads(a)
执行以下代码后:
import json
a = '{"excludeTypes":"*.exe;~\$*.*"}'
json.loads(a)
我得到:
Traceback (most recent call last): File "", line 1, in File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/init.py", line 338, in loads return _default_decoder.decode(s) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 365, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 381, in raw_decode obj, end = self.scan_once(s, idx) ValueError: Expecting property name: line 1 column 2 (char 1)
那么如何将 'a' 转换为字典。
请注意,字符串已经在 'a' 中,我不能在它前面添加 'r'。理想情况下,字符串应该是 {"excludeTypes":"*.exe;~\\$*.*"}
此外,以下代码不起作用:
import json
a = '{"excludeTypes":"*.exe;~\$*.*"}'
b = repr(a)
json.loads(b)
import ast
d = ast.literal_eval(a)
通过转义转义字符“\”:
import json
a = '{"excludeTypes":"*.exe;~\$*.*"}'
a = a.replace("\","\\")
json.loads(a)