Python json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 190)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48140858/
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
json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 190)
提问by SMS
I am running the following code-
我正在运行以下代码-
import json
addrsfile =
open("C:\Users\file.json",
"r")
addrJson = json.loads(addrsfile.read())
addrsfile.close()
if addrJson:
print("yes")
But giving me following error-
但是给了我以下错误-
Traceback (most recent call last):
File "C:/Users/Mayur/Documents/WebPython/Python_WebServices/test.py", line 9, in <module>
addrJson = json.loads(addrsfile.read())
File "C:\Users\Mayur\Anaconda3\lib\json\__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "C:\Users\Mayur\Anaconda3\lib\json\decoder.py", line 342, in decode
raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 190)
Anyone help me please?
有人请帮我吗?
JSON file is like-
JSON 文件就像-
{"name": "XYZ", "address": "54.7168,94.0215", "country_of_residence": "PQR", "countries": "LMN;PQRST", "date": "28-AUG-2008", "type": null}
{"name": "OLMS", "address": null, "country_of_residence": null, "countries": "Not identified;No", "date": "23-FEB-2017", "type": null}
回答by Hannu
You have two records in your json file, and json.loads()
is not able to decode more than one. You need to do it record by record.
您的 json 文件中有两条记录,并且json.loads()
无法解码多条记录。您需要逐条记录。
See Python json.loads shows ValueError: Extra data
参见Python json.loads 显示 ValueError: Extra data
OR you need to reformat your json to contain an array:
或者您需要重新格式化您的 json 以包含一个数组:
{
"foo" : [
{"name": "XYZ", "address": "54.7168,94.0215", "country_of_residence": "PQR", "countries": "LMN;PQRST", "date": "28-AUG-2008", "type": null},
{"name": "OLMS", "address": null, "country_of_residence": null, "countries": "Not identified;No", "date": "23-FEB-2017", "type": null}
]
}
would be acceptable again. But there cannot be several top level objects.
会再次被接受。但是不能有多个顶级对象。
回答by Richard
I was parsing JSON from a REST API call and got this error. It turns out the API had become "fussier" (eg about order of parameters etc) and so was returning malformed results. Check that you are getting what you expect :)
我正在从 REST API 调用解析 JSON 并收到此错误。事实证明,API 变得“更加繁琐”(例如关于参数的顺序等),因此返回格式错误的结果。检查您是否得到了您所期望的 :)