使用 Python 解析无效 JSON - 无效转义导致错误
Parsing invalid JSON with Python - invalid escapes causing errors
我正在解析 'JSON',这通常是正确的,但某些条目包含正则表达式模式,在 json.load
期间会抛出错误
例如
"pattern" : [
{
"data" : ".*\x 39 44 2D 52 51 4D 54 2D 48 46 2D 41 52 4D 30 31 2E 70 64 66 78 \x.*"
}
],
抛出错误:
~/anaconda3/lib/python3.7/json/decoder.py in raw_decode(self, s, idx)
351 """
352 try:
--> 353 obj, end = self.scan_once(s, idx)
354 except StopIteration as err:
355 raise JSONDecodeError("Expecting value", s, err.value) from None
JSONDecodeError: Invalid \escape: line 18419 column 45 (char 595400)
正确的处理方法是什么?
目前我正在尝试(虽然已经尝试了 'unicode-escape' 的其他变体):
with open("json-file.json", "r") as original_file:
file = json.load(original_file)
此处包含的答案适用于无法提前修改的文件(IE,解析前):
How to read from a JSON file with unescaped backslashes?
我正在解析 'JSON',这通常是正确的,但某些条目包含正则表达式模式,在 json.load
例如
"pattern" : [
{
"data" : ".*\x 39 44 2D 52 51 4D 54 2D 48 46 2D 41 52 4D 30 31 2E 70 64 66 78 \x.*"
}
],
抛出错误:
~/anaconda3/lib/python3.7/json/decoder.py in raw_decode(self, s, idx)
351 """
352 try:
--> 353 obj, end = self.scan_once(s, idx)
354 except StopIteration as err:
355 raise JSONDecodeError("Expecting value", s, err.value) from None
JSONDecodeError: Invalid \escape: line 18419 column 45 (char 595400)
正确的处理方法是什么?
目前我正在尝试(虽然已经尝试了 'unicode-escape' 的其他变体):
with open("json-file.json", "r") as original_file:
file = json.load(original_file)
此处包含的答案适用于无法提前修改的文件(IE,解析前):
How to read from a JSON file with unescaped backslashes?