代码:
function exportJson(data, filename = "data.json") {
let jsonStr = JSON.stringify(data, null, 4); // 格式化 JSON 数据
let blob = new Blob([jsonStr], { type: "application/json" });
let a = document.createElement("a");
a.href = URL.createObjectURL(blob);
a.download = filename;
a.click();
URL.revokeObjectURL(a.href);
}
// 示例数据
let gameData = {
level: 1,
score: 1000,
items: ["sword", "shield", "potion"]
};
// 调用导出 JSON
exportJson(gameData);