python怎么验证两个json是不是一样,在Python中解析两个JSON字符串

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])

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值