# 安装:pip install ijson
import ijson
def stream_parse_large_json(file_path):
with open(file_path, 'rb') as f:
# 逐个解析JSON数组中的对象,不加载整个文件
parser = ijson.items(f, 'item')
for obj in parser:
yield obj
# 使用示例
for record in stream_parse_large_json('large_data.json'):
print(record['id']) # 处理每个记录