更新时间:2023-11-01 20:41:58
您可以使用标准的JSON解析器,并利用当正确的JSON字符串后面有额外的数据时引发的描述性异常.
You can use the standard JSON parser and make use of the descriptive exception it throws when there is extra data behind the proper JSON string.
当前(即我的JSON解析器版本)抛出ValueError
并显示如下消息:"Extra data: line 3 column 1 - line 3 column 6 (char 5 - 10)"
.
Currently (that is, my version of the JSON parser) throws a ValueError
with a message looking like this: "Extra data: line 3 column 1 - line 3 column 6 (char 5 - 10)"
.
在这种情况下的数字
The number 5
in this case (you can parse that out of the message easily with a regular expression) provides the information where the parsing failed. So if you get that exception, you can parse a substring of your original input, namely everything up to the character before that, and afterwards (I propose recursively) parse the rest.
import json, re
def jsonMultiParse(s):
try:
return json.loads(s)
except ValueError as problem:
m = re.match(
r'Extra data: line \d+ column \d+ - line \d+ column \d+ .char (\d+) - \d+.',
problem.message)
if not m:
raise
extraStart = int(m.group(1))
return json.loads(s[:extraStart]), jsonMultiParse(s[extraStart:])
print jsonMultiParse('{}[{}] \n\n["foo", 3]')
将打印:
({}, ([{}], [u'foo', 3]))
如果您更喜欢使用直元组而不是嵌套元组:
In case you prefer to get a straight tuple instead of a nested one:
return (json.loads(s),)
和
return (json.loads(s[:extraStart]),) + jsonMultiParse(s[extraStart:])
返回:
({}, [{}], [u'foo', 3])