2015-09-22 64 views
1

考慮這個有效的JSON:的Python解析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

回答

1

實際上它與轉義雙引號無關。見我的測試:

>>> json.loads('{"a": 1, "b": "{\"c\":2}"}') 
Traceback (most recent call last): 
    File "<input>", line 1, in <module> 
    File "/usr/lib/python3.4/json/__init__.py", line 318, in loads 
    return _default_decoder.decode(s) 
    File "/usr/lib/python3.4/json/decoder.py", line 343, in decode 
    obj, end = self.raw_decode(s, idx=_w(s, 0).end()) 
    File "/usr/lib/python3.4/json/decoder.py", line 359, in raw_decode 
    obj, end = self.scan_once(s, idx) 
ValueError: Expecting ',' delimiter: line 1 column 18 (char 17) 

>>> json.loads('{"a": 1, "b": "{"c":2}"}') 
Traceback (most recent call last): 
    File "<input>", line 1, in <module> 
    File "/usr/lib/python3.4/json/__init__.py", line 318, in loads 
    return _default_decoder.decode(s) 
    File "/usr/lib/python3.4/json/decoder.py", line 343, in decode 
    obj, end = self.raw_decode(s, idx=_w(s, 0).end()) 
    File "/usr/lib/python3.4/json/decoder.py", line 359, in raw_decode 
    obj, end = self.scan_once(s, idx) 
ValueError: Expecting ',' delimiter: line 1 column 18 (char 17) 

>>> json.loads('{"a": 1, "b": {"c":2}}') 
{'a': 1, 'b': {'c': 2}} 

>>> json.loads('{"a": 1, "b": {\"c\":2}}') 
{'a': 1, 'b': {'c': 2}} 

>>> 
0

帶引號的字符串內,\"被視爲與常規報價:

>>> '{"a": 1, "b": "{\"c\":2}"}' 
'{"a": 1, "b": "{"c":2}"}' 

其結果是,您的字符串是有效的JSON。

您還需要跳過反斜槓,以便將它們發送到loads。你可以用dumps編碼你想要的字典來看到這個:

>>> json.dumps({"a": 1, "b": "{\"c\": 2}"}) 
'{"a": 1, "b": "{\\"c\\": 2}"}' 

>>> json.loads('{"a": 1, "b": "{\\"c\\": 2}"}') 
{u'a': 1, u'b': u'{"c": 2}'}