A socket receives a JSON formatted string and might receive more than one which will result in a variable containing something like this:
{'a':'1','b':'44'}{'a':'1','b':'44'}
As you can see, it is multiple JSON strings in one variable. How can I decode these in Python?
I mean, is there a way in Python to decode the two JSON strings into an array, or just a way to know there might be two strings in the output?
Using new lines to split them is not a good idea as the data might actually have new lines.
解决方案
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.
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]')
Will print:
({}, ([{}], [u'foo', 3]))
In case you prefer to get a straight tuple instead of a nested one:
return (json.loads(s),)
and
return (json.loads(s[:extraStart]),) + jsonMultiParse(s[extraStart:])
Return:
({}, [{}], [u'foo', 3])