123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- #include "tool.h"
- #include "QtCore/qdir.h"
- #include <QDirIterator>
- const QString tool::dir_path="/users/lal/documents/RedInterstitialData/HotUpdate2";
- QJsonObject tool::generateFolderJson(const QString& folderPath) {
- // QDir folderDir(folderPath);
- // QJsonObject folderObject;
- // folderObject["name"] = folderDir.dirName();
- // QJsonArray childrenArray;
- // // 遍历文件夹中的子项,包括子文件夹和文件
- // QDir::Filters filters = QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot;
- // QDirIterator it(folderPath, filters, QDirIterator::Subdirectories);
- // while (it.hasNext()) {
- // it.next();
- // // 如果是文件夹,递归生成 JSON 数据并添加到子项中
- // if (it.fileInfo().isDir()) {
- // qDebug()<<it.filePath();
- // QJsonObject childObject = generateFolderJson(it.filePath());
- // childrenArray.append(childObject);
- // }
- // // 如果是文件,直接将文件名添加到子项中
- // else {
- // QString fileName = it.fileName();
- // childrenArray.append(fileName);
- // }
- // }
- // folderObject["children"] = childrenArray;
- // return folderObject;
- QDir folderDir(folderPath);
- QJsonObject folderObject;
- folderObject["name"] = folderDir.dirName();
- QJsonArray childrenArray;
- // 遍历文件夹中的子项,包括子文件夹和文件
- QDir::Filters filters = QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot;
- QStringList entries = folderDir.entryList(filters);
- for (const QString& entry : entries) {
- QString entryPath = folderDir.filePath(entry);
- QFileInfo fileInfo(entryPath);
- if (fileInfo.exists() && fileInfo.isDir())
- {
- childrenArray.append(generateFolderJson(entryPath));
- }
- else childrenArray.append(entry);
- }
- folderObject["children"] = childrenArray;
- return folderObject;
- }
- // 生成文件夹的 JSON 文件
- void tool::generateFolderJsonFile(const QString& folderPath, const QString& jsonFilePath) {
- QJsonObject rootObject = tool::generateFolderJson(folderPath);
- QJsonDocument jsonDoc(rootObject);
- QByteArray jsonData = jsonDoc.toJson(QJsonDocument::Indented);
- QFile jsonFile(jsonFilePath);
- if (jsonFile.open(QIODevice::WriteOnly)) {
- jsonFile.write(jsonData);
- jsonFile.close();
- qDebug() << "JSON file generated successfully.";
- } else {
- qDebug() << "Failed to generate JSON file.";
- }
- }
- //获取文件json数据
- QJsonArray tool::getJsanArra(const QString& filePath){
- QFile file(filePath);
- if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
- // 处理文件打开错误
- return QJsonArray();
- }
- // 解析原始 JSON 文件内容
- QByteArray fileData = file.readAll();
- QJsonDocument jsonDoc = QJsonDocument::fromJson(fileData);
- file.close();
- QJsonArray json=jsonDoc.array();
- return json;
- }
- QJsonObject tool::readJsonFile(const QString& filePath)
- {
- // 打开JSON文件
- QFile file(filePath);
- if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
- {
- qDebug() << "无法打开文件:" << file.errorString();
- return QJsonObject();
- }
- // 读取文件内容
- QByteArray jsonData = file.readAll();
- file.close();
- // 解析JSON数据
- QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData);
- if (!jsonDoc.isObject())
- {
- qDebug() << "无效的JSON文件";
- return QJsonObject();;
- }
- // 获取JSON对象
- QJsonObject jsonObj = jsonDoc.object();
- return jsonObj;
- // 处理其他字段...
- }
- QStringList tool::getFilesInFolder(const QString& folderPath)
- {
- QDir folderDir(folderPath);
- QStringList fileList;
- // 获取文件夹中的所有文件
- QStringList nameFilters;
- QDir::Filters filters = QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot;
- QStringList entries = folderDir.entryList(filters);
- qDebug()<<"entries "<<entries;
- for (const QString& entry : entries) {
- QString entryPath = folderDir.filePath(entry);
- QFileInfo fileInfo(entryPath);
- if (fileInfo.exists() && fileInfo.isDir())
- {
- fileList.append(fileInfo.fileName());
- }
- }
- return fileList;
- }
- bool tool::writeJsonFile(const QString& fileName, const QJsonObject& jsonObject)
- {
- QFile file(fileName);
- if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
- {
- // 文件打开失败处理逻辑
- return false;
- }
- QJsonDocument jsonDoc(jsonObject);
- file.write(jsonDoc.toJson());
- file.close();
- return true;
- }
|