【问题标题】:Python breaks parsing json with characters \"Python 用字符 \" 打破解析 json
【发布时间】:2015-12-29 20:40:05
【问题描述】:

我正在尝试使用转义字符解析 json 字符串(我猜是某种)

{
    "publisher": "\"O'Reilly Media, Inc.\""
}

如果我从字符串中删除字符 \",解析器会很好地解析,

不同解析器引发的异常是,

json

  File "/usr/lib/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting , delimiter: line 17 column 20 (char 392)

ujson

ValueError: Unexpected character in found when decoding object value

如何让解析器转义这些字符?

更新: ps。在本例中,json 被导入为 ujson

这是我的ide显示的

逗号是随便加的,json结尾没有逗号,json有效

字符串定义。

【问题讨论】:

  • 你是如何定义输入字符串的?当你 print 字符串时,你还看到反斜杠吗?我根本无法重现。
  • 如果输入字符串在 Python 源代码中,您可能需要先转义那里的元字符。如果是这种情况,请考虑使用原始字符串。
  • @MartijnPieters 逗号被意外添加,问题是字符/"
  • 你能告诉我们你开始解析的方法吗?
  • @Marty:请注意,您的屏幕截图显示您正在定义 Python 字符串,因此 \" 被解释为 Python 字符串文字转义 .您产生的实际值中的反冲消失了。

标签: python json parsing ujson


【解决方案1】:

您的 JSON 无效。如果您对 JSON 对象有任何疑问,可以随时使用JSONlint 验证它们。在您的情况下,您有一个对象

{
"publisher": "\"O'Reilly Media, Inc.\"",
}

你有一个额外的逗号表示应该有别的东西。所以 JSONlint 产生

第 2 行的解析错误: ...edia, Inc.\"", } ---------------------^ 期待“字符串”

这将开始帮助您找到错误所在。

去掉逗号

{
"publisher": "\"O'Reilly Media, Inc.\""
}

产量

有效的 JSON

更新:我将这些内容保留在 JSONlint 中,因为它可能在未来对其他人有所帮助。至于你格式良好的 JSON 对象,我有

import json

d = {
    "publisher": "\"O'Reilly Media, Inc.\""
    }

print "Here is your string parsed."
print(json.dumps(d))

屈服

这是你的字符串解析。 {"publisher": "\"O'Reilly Media, Inc.\""}

进程以退出代码 0 结束

【讨论】:

  • 对不起,后面的逗号不小心,json是有效的,已经发了截图。
【解决方案2】:

您几乎可以肯定没有正确定义转义的反斜杠。如果您正确定义字符串,JSON 解析 就好了

>>> import json
>>> json_str = r'''
... {
...     "publisher": "\"O'Reilly Media, Inc.\""
... }
... '''  # raw string to prevent the \" from being interpreted by Python
>>> json.loads(json_str)
{u'publisher': u'"O\'Reilly Media, Inc."'}

请注意,我使用 原始字符串文字 来定义 Python 中的字符串;如果我不这样做,\" 将由 Python 解释并插入常规的 "。否则,您必须将反斜杠 加倍

>>> print '\"'
"
>>> print '\\"'
\"
>>> print r'\"'
\"

将解析后的 Python 结构重新编码回 JSON 会显示反斜杠重新出现,字符串的 repr() 输出使用相同的双反斜杠:

>>> json.dumps(json.loads(json_str))
'{"publisher": "\\"O\'Reilly Media, Inc.\\""}'
>>> print json.dumps(json.loads(json_str))
{"publisher": "\"O'Reilly Media, Inc.\""}

如果您没有转义 \ 转义,您最终会得到未转义的引号:

>>> json_str_improper = '''
... {
...     "publisher": "\"O'Reilly Media, Inc.\""
... }
... '''
>>> print json_str_improper

{
    "publisher": ""O'Reilly Media, Inc.""
}

>>> json.loads(json_str_improper)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python2.7/json/decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python2.7/json/decoder.py", line 382, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting , delimiter: line 3 column 20 (char 22)

注意\" 序列现在打印为",反斜杠消失了!

【讨论】:

  • 已经更新了问题,抱歉后面的逗号不小心,json是有效的。
  • 谢谢,根据这个答案将字符串定义为原始(r)解决了这个问题。请注意问题的错误消息已被弄乱,已更新。
猜你喜欢
  • 1970-01-01
  • 2021-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多