Python JSON 解码 ValueError:额外数据:
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14150823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python JSON decode ValueError: Extra data:
提问by Jeremy
I'm having a problem trying to decode and print JSON I receive from a socket connection.
我在尝试解码和打印从套接字连接收到的 JSON 时遇到问题。
The full traceback:
完整的追溯:
C:\Users\Jeremy>python F:\Files\Python\test.py
2013-01-04 21:15:35 [INFO] [AutoSaveWorld] World save Complete!
2013-01-04 21:15:50 [INFO] [←[34;1mMain←[37;1m]←[32;22mRexOZ←[37;1m: you cahaned
your house it looks awesome←[m
Traceback (most recent call last):
File "F:\Files\Its safer indoors\Python\test.py", line 14, in <module>
data = json.loads(dreceve)
File "C:\Python33\lib\json\__init__.py", line 309, in loads
return _default_decoder.decode(s)
File "C:\Python33\lib\json\decoder.py", line 355, in decode
raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 2 column 1 - line 3 column 1 (char 151 - 344)
As you can see the first 2 lines print fine and then it crashes.
如您所见,前 2 行打印正常,然后崩溃。
Full code:
完整代码:
import socket
import json
import re
HOST = 'host.host.net'
PORT = 8082
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
dsend = "/api/subscribe?source=console&key=SUPERSEXYSECRETEY&show_previous=true\n"
s.sendall(dsend.encode())
while 1:
dreceve = s.recv(1024).decode()
data = json.loads(dreceve)
succses = data['success']
line = succses['line']
print(line)
s.close()
I have goggled around for this error and the pages I found did not solve my problem, any help work be appreciated.
我已经搜索了这个错误,我发现的页面没有解决我的问题,任何帮助工作不胜感激。
回答by miku
Whatever you receive, it does not seem to end where it should end; example:
无论你收到什么,它似乎都没有在它应该结束的地方结束;例子:
>>> import json
>>> json.loads(""" {"Hello" : "World"} \ """)
....
ValueError: Extra data: line 1 column 21 - line 1 column 23 (char 21 - 23)
I'd suggest inspect your output before it gets parsed to get hold of the problem.
我建议在解析您的输出之前检查您的输出以解决问题。
PS. There are simpler ways to get JSON data from a server (assuming your server returns parsable JSON, which it might not). Here is an example using the requestslibrary:
附注。有一些更简单的方法可以从服务器获取 JSON 数据(假设您的服务器返回可解析的 JSON,但它可能不会)。这是使用请求库的示例:
>>> import json, requests
>>> u = "http://gdata.youtube.com/feeds/api/standardfeeds/most_popular?alt=json"
>>> json.loads(requests.get(u).text) # <-- request + parse
{u'feed': {u'category': [{u'term': u'http://gdata.youtube.com/...
.....
.....
回答by VxCoder
In the json lib ->decoder.py->decode function
在 json lib ->decoder.py->decode 函数中
if end != len(s):
raise ValueError(errmsg("Extra data" , s , end , len(s)))
It's mean if your string's len != end , will raise this Exception And the end is the last "}" postion in your string. so you I can use:
这意味着如果您的字符串的 len != end 会引发此异常,并且 end 是您字符串中的最后一个 "}" 位置。所以你我可以使用:
string = "".join([string.rsplit("}" , 1)[0] , "}"])
cut the extra data after the last "}".
删除最后一个“}”之后的额外数据。
回答by bl79
Sometimes there may be ';' at tail of the JSON string. Just remove it:
有时可能会有';' 在 JSON 字符串的尾部。只需删除它:
json.loads(string.strip(';'))

