我正在运行以下代码-
import json
addrsfile =
open("C:\\Users\file.json",
"r")
addrJson = json.loads(addrsfile.read())
addrsfile.close()
if addrJson:
print("yes")
但给了我以下错误-
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)
有人能帮帮我吗?
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}
发布于 2018-01-09 01:04:13
您的json文件中有两条记录,而json.loads()
无法解码多条记录。你需要一张一张地做。
请参阅Python json.loads shows ValueError: Extra data
或者你需要重新格式化你的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}
]
}
将会再次被接受。但是不能有几个顶级对象。
发布于 2019-02-04 02:24:22
我从REST API调用解析JSON时遇到了这个错误。事实证明,API变得更加“繁琐”(例如,关于参数的顺序等),因此返回的结果也是错误的。检查您是否得到了预期的结果:)
发布于 2020-10-24 23:37:45
如果字符串中存在json.loads()
无法识别的部分,也会显示此错误。在此示例字符串中,将在第27个字符(char 27)
处引发错误。
string = """[{"Item1": "One", "Item2": False}, {"Item3": "Three"}]"""
我的解决方案是使用string.replace()
将这些项转换为字符串:
import json
string = """[{"Item1": "One", "Item2": False}, {"Item3": "Three"}]"""
string = string.replace("False", '"False"')
dict_list = json.loads(string)
https://stackoverflow.com/questions/48140858
复制相似问题