tool.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include "tool.h"
  2. #include "QtCore/qdir.h"
  3. #include <QDirIterator>
  4. const QString tool::dir_path="/users/lal/documents/RedInterstitialData/HotUpdate2";
  5. QJsonObject tool::generateFolderJson(const QString& folderPath) {
  6. // QDir folderDir(folderPath);
  7. // QJsonObject folderObject;
  8. // folderObject["name"] = folderDir.dirName();
  9. // QJsonArray childrenArray;
  10. // // 遍历文件夹中的子项,包括子文件夹和文件
  11. // QDir::Filters filters = QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot;
  12. // QDirIterator it(folderPath, filters, QDirIterator::Subdirectories);
  13. // while (it.hasNext()) {
  14. // it.next();
  15. // // 如果是文件夹,递归生成 JSON 数据并添加到子项中
  16. // if (it.fileInfo().isDir()) {
  17. // qDebug()<<it.filePath();
  18. // QJsonObject childObject = generateFolderJson(it.filePath());
  19. // childrenArray.append(childObject);
  20. // }
  21. // // 如果是文件,直接将文件名添加到子项中
  22. // else {
  23. // QString fileName = it.fileName();
  24. // childrenArray.append(fileName);
  25. // }
  26. // }
  27. // folderObject["children"] = childrenArray;
  28. // return folderObject;
  29. QDir folderDir(folderPath);
  30. QJsonObject folderObject;
  31. folderObject["name"] = folderDir.dirName();
  32. QJsonArray childrenArray;
  33. // 遍历文件夹中的子项,包括子文件夹和文件
  34. QDir::Filters filters = QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot;
  35. QStringList entries = folderDir.entryList(filters);
  36. for (const QString& entry : entries) {
  37. QString entryPath = folderDir.filePath(entry);
  38. QFileInfo fileInfo(entryPath);
  39. if (fileInfo.exists() && fileInfo.isDir())
  40. {
  41. childrenArray.append(generateFolderJson(entryPath));
  42. }
  43. else childrenArray.append(entry);
  44. }
  45. folderObject["children"] = childrenArray;
  46. return folderObject;
  47. }
  48. // 生成文件夹的 JSON 文件
  49. void tool::generateFolderJsonFile(const QString& folderPath, const QString& jsonFilePath) {
  50. QJsonObject rootObject = tool::generateFolderJson(folderPath);
  51. QJsonDocument jsonDoc(rootObject);
  52. QByteArray jsonData = jsonDoc.toJson(QJsonDocument::Indented);
  53. QFile jsonFile(jsonFilePath);
  54. if (jsonFile.open(QIODevice::WriteOnly)) {
  55. jsonFile.write(jsonData);
  56. jsonFile.close();
  57. qDebug() << "JSON file generated successfully.";
  58. } else {
  59. qDebug() << "Failed to generate JSON file.";
  60. }
  61. }
  62. //获取文件json数据
  63. QJsonArray tool::getJsanArra(const QString& filePath){
  64. QFile file(filePath);
  65. if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
  66. // 处理文件打开错误
  67. return QJsonArray();
  68. }
  69. // 解析原始 JSON 文件内容
  70. QByteArray fileData = file.readAll();
  71. QJsonDocument jsonDoc = QJsonDocument::fromJson(fileData);
  72. file.close();
  73. QJsonArray json=jsonDoc.array();
  74. return json;
  75. }
  76. QJsonObject tool::readJsonFile(const QString& filePath)
  77. {
  78. // 打开JSON文件
  79. QFile file(filePath);
  80. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
  81. {
  82. qDebug() << "无法打开文件:" << file.errorString();
  83. return QJsonObject();
  84. }
  85. // 读取文件内容
  86. QByteArray jsonData = file.readAll();
  87. file.close();
  88. // 解析JSON数据
  89. QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData);
  90. if (!jsonDoc.isObject())
  91. {
  92. qDebug() << "无效的JSON文件";
  93. return QJsonObject();;
  94. }
  95. // 获取JSON对象
  96. QJsonObject jsonObj = jsonDoc.object();
  97. return jsonObj;
  98. // 处理其他字段...
  99. }
  100. QStringList tool::getFilesInFolder(const QString& folderPath)
  101. {
  102. QDir folderDir(folderPath);
  103. QStringList fileList;
  104. // 获取文件夹中的所有文件
  105. QStringList nameFilters;
  106. QDir::Filters filters = QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot;
  107. QStringList entries = folderDir.entryList(filters);
  108. qDebug()<<"entries "<<entries;
  109. for (const QString& entry : entries) {
  110. QString entryPath = folderDir.filePath(entry);
  111. QFileInfo fileInfo(entryPath);
  112. if (fileInfo.exists() && fileInfo.isDir())
  113. {
  114. fileList.append(fileInfo.fileName());
  115. }
  116. }
  117. return fileList;
  118. }
  119. bool tool::writeJsonFile(const QString& fileName, const QJsonObject& jsonObject)
  120. {
  121. QFile file(fileName);
  122. if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
  123. {
  124. // 文件打开失败处理逻辑
  125. return false;
  126. }
  127. QJsonDocument jsonDoc(jsonObject);
  128. file.write(jsonDoc.toJson());
  129. file.close();
  130. return true;
  131. }