此代码段引发错误。
while True:
data = s.recv(1024)
js = json.loads(data)
print js
> Traceback (most recent call last):
File "test.py", line 10, in <module>
js = json.loads(data)
File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 367, in decode
raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 9 column 3 - line 9 column 4 (char 303 - 304)
这是json:
{"B49":{
"Time":1533208501.720,
"PacketID":2358,
"Loss":0,
"RSSI":-57,
"*****":[50.4,49.8,49.8,51,51.7,49.1,49.8,49.8,50.1,48.5],
"*****":[-10.1,-9.8,-9.8,-7.9,-11.4,-9.2,-9.8,-10.1,-9.5,-9.2],
"*****":[-23.6,-21.2,-21.3,-20.9,-20.5,-22.4,-23.8,-23.4,-21.5,-23.9]
}}
我不明白,因为当我尝试type(data)
时会得到type<str>
,这意味着我正在按照此方法的要求传递字符串参数。
答案 0 :(得分:0)
您看到的错误完全表明在您收到该json的}
缓冲区中关闭data
后有一些字符。
这是我的考试:
In [3]: x = '''{"B49":{
"Time":1533208501.720,
"PacketID":2358,
"Loss":0,
"RSSI":-57,
"MagnetometerX":[50.4,49.8,49.8,51,51.7,49.1,49.8,49.8,50.1,48.5],
"MagnetometerY":[-10.1,-9.8,-9.8,-7.9,-11.4,-9.2,-9.8,-10.1,-9.5,-9.2],
"MagnetometerZ":[-23.6,-21.2,-21.3,-20.9,-20.5,-22.4,-23.8,-23.4,-21.5,-23.9]
}}'''
In [4]: json.loads(x)
Out[4]:
{'B49': {'Time': 1533208501.72,
'PacketID': 2358,
'Loss': 0,
'RSSI': -57,
'MagnetometerX': [50.4, 49.8, 49.8, 51, 51.7, 49.1, 49.8, 49.8, 50.1, 48.5],
'MagnetometerY': [-10.1,
-9.8,
-9.8,
-7.9,
-11.4,
-9.2,
-9.8,
-10.1,
-9.5,
-9.2],
'MagnetometerZ': [-23.6,
-21.2,
-21.3,
-20.9,
-20.5,
-22.4,
-23.8,
-23.4,
-21.5,
-23.9]}}
所以这正常工作。但是看看当我在json数据的末尾添加\0
时会发生什么:
In [5]: x1= '''{"B49":{
"Time":1533208501.720,
"PacketID":2358,
"Loss":0,
"RSSI":-57,
"MagnetometerX":[50.4,49.8,49.8,51,51.7,49.1,49.8,49.8,50.1,48.5],
"MagnetometerY":[-10.1,-9.8,-9.8,-7.9,-11.4,-9.2,-9.8,-10.1,-9.5,-9.2],
"MagnetometerZ":[-23.6,-21.2,-21.3,-20.9,-20.5,-22.4,-23.8,-23.4,-21.5,-23.9]
}}\0'''
In [6]: json.loads(x)
---------------------------------------------------------------------------
JSONDecodeError Traceback (most recent call last)
<ipython-input-18-f896d9a0a2ca> in <module>()
----> 1 json.loads(x)
~\AppData\Local\Continuum\anaconda3\lib\json\__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
352 parse_int is None and parse_float is None and
353 parse_constant is None and object_pairs_hook is None and not kw):
--> 354 return _default_decoder.decode(s)
355 if cls is None:
356 cls = JSONDecoder
~\AppData\Local\Continuum\anaconda3\lib\json\decoder.py in decode(self, s, _w)
340 end = _w(s, end).end()
341 if end != len(s):
--> 342 raise JSONDecodeError("Extra data", s, end)
343 return obj
344
JSONDecodeError: Extra data: line 9 column 3 (char 290)
这意味着,如果在接收的json末尾还有其他空字符或任何其他字符,则会出现此错误。很有可能您使用来自外部系统的socket.recv接收到json,后缀为0,并且在尝试解析为json之前应先删除它。
答案 1 :(得分:0)
显然最后一个字符无法识别,并且不能作为参数传递给json.loads方法,因此我这样做是想摆脱数据str对象的最后一个字符,然后它可以正常工作。
while True:
data = s.recv(1024)
j=json.loads(data [0:(len(data)-1)]) #trunking the last character of data otherwise is not a valid argument for loads