如何修复JSONDecodeError: Extra data(行1列8)错误?
解决JSONDecodeError: Extra data错误的方案
问题根源
你的JSON文件不符合标准格式——合法的JSON对象必须用{}包裹所有键值对,而当前文件只有零散的键值对,缺少外层大括号,导致解析器判定存在“多余数据”。
具体修复方法
1. 修正JSON文件结构
将Post.json的内容修改为标准JSON格式:
{ "name": "Test", "job": "Programmer" }
2. 优化Python代码(可选)
使用with语句自动管理文件资源,避免手动关闭文件的疏漏:
import json with open("C:\\Users\\User\\Desktop\\Post.json", 'r') as file: request_json = json.load(file) print(request_json)
特殊场景处理(如果文件是多行独立JSON)
如果你的文件是每行一个独立JSON对象(比如日志格式),可以逐行解析:
import json parsed_data = [] with open("C:\\Users\\User\\Desktop\\Post.json", 'r') as file: for line in file: stripped_line = line.strip() if stripped_line: parsed_data.append(json.loads(stripped_line)) print(parsed_data)
内容的提问来源于stack exchange,提问作者Ron
相关产品推荐
相关产品推荐

