【问题标题】:Safely parsing nested json double quotes安全解析嵌套的 json 双引号
【发布时间】:2021-08-23 11:59:27
【问题描述】:

问题:

>>> import json
>>> json_str='{"message": "John said "come here!""}'
>>> json.loads(json_str)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/ram/.pyenv/versions/3.9.6/lib/python3.9/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/Users/ram/.pyenv/versions/3.9.6/lib/python3.9/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/Users/ram/.pyenv/versions/3.9.6/lib/python3.9/json/decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 25 (char 24)

然后我尝试转义双引号。像这样:

>>> json_str='{"message": "John said \"come here!\""}'
>>> json.loads(json_str)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/ram/.pyenv/versions/3.9.6/lib/python3.9/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/Users/ram/.pyenv/versions/3.9.6/lib/python3.9/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/Users/ram/.pyenv/versions/3.9.6/lib/python3.9/json/decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 25 (char 24)

以下是可行的:

>>> json_str='{"message": "John said \'come here!\'"}'
>>> json.loads(json_str)
{'message': "John said 'come here!'"}

我做错了什么?是否可以将 json 库标记为更宽容(帮助我将 json 解析留给 json 库)?

PS:在回答here之后,我尝试了以下操作:

>>> data=json.dumps('{"message":"John said "hello!""}')
>>> print(data)
"{\"message\":\"John said \"hello!\"\"}"
>>> dict = json.loads(data)
>>> print(dict)
{"message":"John said "hello!""}
>>> print(dict['message'])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: string indices must be integers
>>> print(dict[0])
{

最后一个 json.loads 返回一个字符串而不是 dict。但数据中正确转义的引号听起来很有希望。

【问题讨论】:

    标签: json python-3.x python-3.9


    【解决方案1】:

    您可以简单地使用原始字符串或双反斜杠,以便在 python 和 json 字符串中转义反斜杠,以便json.loads 可以看到反斜杠。

    >>> json.loads(r'{"message": "John said \"come here!\""}')
    {'message': 'John said "come here!"'}
    >>> json.loads('{"message": "John said \\"come here!\\""}')
    {'message': 'John said "come here!"'}
    

    【讨论】:

      猜你喜欢
      • 2021-11-11
      • 1970-01-01
      • 1970-01-01
      • 2016-07-02
      • 1970-01-01
      • 2014-03-27
      • 1970-01-01
      • 2013-07-03
      • 2013-07-09
      相关资源
      最近更新 更多