当前位置: 首页> 游戏> 单机 > 多个JSON文件中目标Key值检索

多个JSON文件中目标Key值检索

时间:2025/7/9 7:47:26来源:https://blog.csdn.net/weixin_61933613/article/details/139455676 浏览次数:0次

摘要:工作中需要将Excel中的文件名,与JSON文件逐个匹配,并查找其中目标key的值是否满足条件,尝试写了个JS脚本实现下...

1. 安装xlsx库

        首先,需要安装xlsx库,允许用户在Node.js和浏览器环境中读写Excel文件。安装步骤简单

npm install xlsx

2. 创建脚本逐步实现功能

2.1 从Excel文件读取文件名并与JSON文件匹配

        由于Excel中文件名称不完整,需要处理后,才能与JSON文件名称正确匹配

// 从Excel文件读取文件名模式
async function getFileNamesFromExcel(excelFilePath) {// 读取Excel文件const workbook = XLSX.readFile(excelFilePath);// 获取第一个工作表const sheetName = workbook.SheetNames[0];const sheet = workbook.Sheets[sheetName];// 将工作表转换为JSON对象数组const data = XLSX.utils.sheet_to_json(sheet, { header: 1 });// 提取文件名列的值作为文件名模式数组const patterns = data.map(row => {return '前缀'+row[10]+ '.json';});return patterns;
}
2.2 读取JSON文件检查是否满足条件

        按照Excel中匹配后的文件名,这个查找JSON文件,并判断是否满足条件

// 读取并检查JSON文件
async function checkSpecifiedJsonFiles(fileNamesFromExcel, configsDir) {for (const fileNameFromExcel of fileNamesFromExcel) {const jsonFileName = fileNameFromExcel; //const filePath = path.join(configsDir, jsonFileName);try {// 读取文件const content = await fs.readFile(filePath, 'utf8');const config = JSON.parse(content);// 这里定义需求:检查 "targetKey1" 和 "targetKey2" 字段const targetKey1 = config.targetKey1;const targetKey2 = config.targetKey2;if (targetKey1 === true || targetKey1 === true) {console.log(`文件 ${jsonFileName} 不满足需求。`);} else {console.log(`文件 ${jsonFileName} 满足需求。`);}} catch (error) {if (error.code === 'ENOENT') {// 文件不存在console.log(`文件 ${jsonFileName} 不存在。`);} else {// 其他错误console.error(`处理文件 ${jsonFileName} 时发生错误:`, error);}}}
}// 主函数
async function main() {try {const fileNames = await getFileNamesFromExcel(excelFilePath);await checkSpecifiedJsonFiles(fileNames, configsDir);} catch (error) {console.error('发生错误:', error);}
}

        完整脚本:

const fs = require('fs').promises;
const path = require('path');
const XLSX = require('xlsx');// 假设Excel文件名为fileNames.xlsx, 并且JSONChecker和JSONDirector位于同一目录下
const excelFilePath = '/Users/JSONChecker/fileNames.xlsx';
const configsDir = '/Users/JSONDirectory';  // JSON配置文件的目录(绝对路径)// 从Excel文件读取文件名模式
async function getFileNamesFromExcel(excelFilePath) {// 读取Excel文件const workbook = XLSX.readFile(excelFilePath);// 获取第一个工作表const sheetName = workbook.SheetNames[0];const sheet = workbook.Sheets[sheetName];// 将工作表转换为JSON对象数组const data = XLSX.utils.sheet_to_json(sheet, { header: 1 });// 提取row[10]列的值作为文件名模式数组const patterns = data.map(row => {return '前缀'+row[10]+ '.json';});return patterns;
}// 读取并检查JSON文件
async function checkSpecifiedJsonFiles(fileNamesFromExcel, configsDir) {for (const fileNameFromExcel of fileNamesFromExcel) {const jsonFileName = fileNameFromExcel; //const filePath = path.join(configsDir, jsonFileName);try {// 读取文件const content = await fs.readFile(filePath, 'utf8');const config = JSON.parse(content);// 这里定义需求:检查 "targetKey1" 和 "targetKey2" 字段const targetKey1 = config.targetKey1;const targetKey2 = config.targetKey2;if (targetKey1 === true || targetKey1 === true) {console.log(`文件 ${jsonFileName} 不满足需求。`);} else {console.log(`文件 ${jsonFileName} 满足需求。`);}} catch (error) {if (error.code === 'ENOENT') {// 文件不存在console.log(`文件 ${jsonFileName} 不存在。`);} else {// 其他错误console.error(`处理文件 ${jsonFileName} 时发生错误:`, error);}}}
}// 主函数
async function main() {try {const fileNames = await getFileNamesFromExcel(excelFilePath);await checkSpecifiedJsonFiles(fileNames, configsDir);} catch (error) {console.error('发生错误:', error);}
}// 执行主函数
main();

3. 总结

        将Excel文件保存为fileNames.xlsx,并确保它位于脚本所在目录下。Excel文件应该有一个列row[10],其中包含需要检查的JSON文件的名称。运行上述脚本即可按照Excel中文件顺序输出结果

node parentDirectory/JSONChecker/JSONChecker.js

关键字:多个JSON文件中目标Key值检索

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: