Python 中断解析 json 字符 \"
Python breaks parsing json with characters \"
我正在尝试用转义字符解析 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 显示的内容
逗号是accide刚加的,json末尾没有逗号,json有效
字符串定义。
您几乎可以肯定没有定义正确转义的反斜杠。如果你正确地定义了字符串,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 解释并插入一个常规的 "
。你必须 double 反斜杠否则:
>>> 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无效。如果您对 JSON 对象有疑问,可以随时使用 JSONlint 验证它们。在你的情况下你有一个对象
{
"publisher": "\"O'Reilly Media, Inc.\"",
}
你有一个额外的逗号,表示应该有其他内容。所以 JSONlint 产量
Parse error on line 2:
...edia, Inc.\"", }
---------------------^
Expecting 'STRING'
这将开始帮助您找到错误所在。
删除
的逗号
{
"publisher": "\"O'Reilly Media, Inc.\""
}
产量
Valid JSON
更新:我将这些内容保留在 JSONlint 中,因为它可能对将来的其他人有所帮助。至于你的格式正确的 JSON 对象,我有
import json
d = {
"publisher": "\"O'Reilly Media, Inc.\""
}
print "Here is your string parsed."
print(json.dumps(d))
屈服
Here is your string parsed.
{"publisher": "\"O'Reilly Media, Inc.\""}
Process finished with exit code 0
我正在尝试用转义字符解析 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
如何让解析器转义这些字符?
更新:
这是我的 ide 显示的内容
逗号是accide刚加的,json末尾没有逗号,json有效
字符串定义。
您几乎可以肯定没有定义正确转义的反斜杠。如果你正确地定义了字符串,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 解释并插入一个常规的 "
。你必须 double 反斜杠否则:
>>> 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无效。如果您对 JSON 对象有疑问,可以随时使用 JSONlint 验证它们。在你的情况下你有一个对象
{
"publisher": "\"O'Reilly Media, Inc.\"",
}
你有一个额外的逗号,表示应该有其他内容。所以 JSONlint 产量
Parse error on line 2: ...edia, Inc.\"", } ---------------------^ Expecting 'STRING'
这将开始帮助您找到错误所在。
删除
的逗号{
"publisher": "\"O'Reilly Media, Inc.\""
}
产量
Valid JSON
更新:我将这些内容保留在 JSONlint 中,因为它可能对将来的其他人有所帮助。至于你的格式正确的 JSON 对象,我有
import json
d = {
"publisher": "\"O'Reilly Media, Inc.\""
}
print "Here is your string parsed."
print(json.dumps(d))
屈服
Here is your string parsed. {"publisher": "\"O'Reilly Media, Inc.\""}
Process finished with exit code 0