考虑以下有效的json内容:
{"a": 1, "b": "{\"c\":2}"}
使用Python的json模块进行解析时会出现错误——看起来是由于 \" 导致程序无法解析:
json.loads('{"a": 1, "b": "{\"c\":2}"}') 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 , delimiter: line 1 column 15 (char 14)
是否有任何方法可以在Python中解析此内容,无论是使用json模块还是其他模块如ujson?
{"a": 1, "b": "{\"c\":2}"}
使用Python的json模块进行解析时会出现错误——看起来是由于 \" 导致程序无法解析:
json.loads('{"a": 1, "b": "{\"c\":2}"}') 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 , delimiter: line 1 column 15 (char 14)
是否有任何方法可以在Python中解析此内容,无论是使用json模块还是其他模块如ujson?
json.loads(r'{"a": 1, "b": "{\"c\":2}"}')
。 - Martijn Pieters