简体   繁体   English

如何将此json字符串转换为dict?

[英]How to convert this json string to dict?

After executing the following code: 执行以下代码后:

import json
a = '{"excludeTypes":"*.exe;~\\$*.*"}'
json.loads(a)

I get: 我得到:

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) 追溯(最近一次通话最后一次):文件“ /”/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/ init .py”中第1行,第338行,位于加载返回_default_decoder.decode(s)文件“ /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py”,行365,在解码obj中,end = self.raw_decode (s,idx = _w(s,0).end())文件“ /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py”,第381行,在raw_decode obj,end = self.scan_once(s,idx)ValueError:期望的属性名称:第1行第2列(字符1)

So how can I convert 'a' to dict. 那么如何将“ a”转换为dict。 Please note that the string is already in 'a' and I cannot add 'r' in front of it. 请注意,该字符串已经在'a'中,并且我不能在其前面添加'r'。 Ideally, the string should have been {"excludeTypes":"*.exe;~\\\\\\\\$*.*"} 理想情况下,字符串应为{"excludeTypes":"*.exe;~\\\\\\\\$*.*"}

Also, the following code doesn't work: 另外,以下代码不起作用:

import json
a = '{"excludeTypes":"*.exe;~\\$*.*"}'
b = repr(a)
json.loads(b)
import ast

d = ast.literal_eval(a)

By escaping Escape Character "\\": 通过转义转义字符“ \\”:

import json
a = '{"excludeTypes":"*.exe;~\\$*.*"}'
a = a.replace("\\","\\\\")
json.loads(a)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM