python - 加载 json 文件时出现 Json 解码器错误。虽然文件被验证为 Json。在 Python 中
问题描述
所以我正在打开一个 Json 文件,当我尝试将文件加载到变量时,我得到一个错误,因为它无法读取文件。虽然我已经(在线)验证了 Json 文件是有效的。我正在使用这段代码:
with open("messagesTest2.json") as json_file:
data = json.load(json_file) <----- ERROR
for p in data['commits']:
print(p['message'])
我得到这个错误。虽然我有另一个也经过验证的 json 文件并且此代码有效。但是这个文件不起作用。我的猜测是文件中的某处无法翻译为 json?是解码器的问题吗?
在 raw_decode 从 None json.decoder.JSONDecodeError 中引发 JSONDecodeError("Expecting value", s, err.value): Expecting value: line 1 column 1 (char 0)
你知道如何解决它吗?请记住,json 文件是有效的,否则我必须显示该文件,但我必须隐藏一些数据:D
Json 文件(urls/passwards/logins/etc 已被替换,但格式保持不变):
{
"commits": [{
"sha": "asjdaskldjkalsk",
"node_id": "sakldjaskldjaskldjklas",
"commit": {
"author": {
"name": "korki",
"email": "korki@kth.se",
"date": "2015-09-07T22:06:51Z"
},
"committer": {
"name": "korki",
"email": "korki@kth.se",
"date": "2015-09-07T22:06:51Z"
},
"message": "Added LaTex template and instructions",
"tree": {
"sha": "askdljaskdlajsklda",
"url": "https://gits-15.ds.sd.se/04dd5b226dda1915"
},
"url": "https://gits-15.ds.sd.se/04dd5b226dda1915",
"comment_count": 0,
"verification": {
"verified": "False",
"reason": "unsigned",
"signature": "None",
"payload": "None"
}
},
"url": "https://gits-15.ds.sd.se/04dd5b226dda1915",
"html_url": "https://gits-15.ds.sd.se/04dd5b226dda1915",
"comments_url": "https://gits-15.ds.sd.se/04dd5b226dda1915",
"author": {
"login": "korki",
"id": 999,
"node_id": "askljdklas==",
"type": "User",
"site_admin": "None"
},
"committer": {
"login": "korki",
"id": 999,
"node_id": "askljdklas==",
"type": "User",
"site_admin": "None"
},
"parents": [{
"sha": "asdaskldjasdklsjl",
"url": "https://gits-15.ds.sd.se/04dd5b226dda1915",
"html_url": "https://gits-15.ds.sd.se/04dd5b226dda1915"
}]
}, {
"sha": "kasdjklasdjklas",
"node_id": "sdklasjdklasjkl",
"commit": {
"author": {
"name": "korki",
"email": "korki@kth.se",
"date": "2015-08-31T10:45:24Z"
},
"committer": {
"name": "korki",
"email": "korki@kth.se",
"date": "2015-08-31T10:45:24Z"
},
"message": "Update README.md",
"tree": {
"sha": "askldjkasldjklas",
"url": "https://gits-15.ds.sd.se/04dd5b226dda1915"
},
"url": "https://gits-15.ds.sd.se/04dd5b226dda1915",
"comment_count": 0,
"verification": {
"verified": "None",
"reason": "unsigned",
"signature": "None",
"payload": "None"
}
},
"url": "https://gits-15.ds.sd.se/04dd5b226dda1915",
"html_url": "https://gits-15.ds.sd.se/04dd5b226dda1915",
"comments_url": "https://gits-15.ds.sd.se/04dd5b226dda1915",
"author": {
"login": "korki",
"id": 999,
"node_id": "dkasdasdnas==",
"type": "User",
"site_admin": "None"
},
"committer": {
"login": "korki",
"id": 999,
"node_id": "askldaskldja==",
"type": "User",
"site_admin": "None"
},
"parents": [{
"sha": "dlkasdjklas;dlkjas;",
"url": "https://gits-15.ds.sd.se/04dd5b226dda1915",
"html_url": "https://gits-15.ds.sd.se/04dd5b226dda1915"
}]
}, {
"sha": "dsagadsgsgdsa",
"node_id": "sdagfsdgsd",
"commit": {
"author": {
"name": "korki",
"email": "korki@kth.se",
"date": "2015-08-31T10:44:42Z"
},
"committer": {
"name": "korki",
"email": "korki@kth.se",
"date": "2015-08-31T10:44:42Z"
},
"message": "Initial commit",
"tree": {
"sha": "asdasddasdas",
"url": "https://gits-15.ds.sd.se/04dd5b226dda1915"
},
"url": "https://gits-15.ds.sd.se/04dd5b226dda1915",
"comment_count": 0,
"verification": {
"verified": "None",
"reason": "unsigned",
"signature": "None",
"payload": "None"
}
},
"url": "https://gits-15.ds.sd.se/04dd5b226dda1915",
"html_url": "https://gits-15.ds.sd.se/04dd5b226dda1915",
"comments_url": "https://gits-15.ds.sd.se/04dd5b226dda1915",
"author": {
"login": "korki",
"id": 999,
"node_id": "kjklklj==",
"type": "User",
"site_admin": "None"
},
"committer": {
"login": "korki",
"id": 999,
"node_id": "jhkjkj==",
"gravatar_id": "",
"type": "User",
"site_admin": "None"
},
"parents": []
}]
}
解决方案
该错误意味着它正在读取一个空白文件。确保您正在阅读您认为正在阅读的文件。
编辑:另一种可能性是您已经阅读了文件的所有行。如果您通读所有行并尝试读取该文件,它将显示为一个空白文件。
推荐阅读
- asp.net - IIS 不提供任何静态文件
- glog - 如何在一个进程中使用 GLOG 将日志写入不同的日志文件
- python - Python 将日期时间索引(复数索引)转换为多个日期时间范围
- python - 主线程执行时不显示进度指示器
- cmake - CMake add_library 与未来的来源
- sql - Sqlite 连接两个表
- python - ModuleNotFoundError:没有名为“tensorflow.python.keras.applications.MobileNetV2”的模块
- python - 显示列表元素,就好像它们是输入的一样
- javascript - 具有对象的数组 -> 格式化为字符串的数组
- kubernetes - 如何从 Grafana 仪表板中隐藏已终止的 pod