我是第一次与https://github.com/nlohmann/json合作,我必须像这样创建文件层次结构:
{
"Files": [
{
"Name": "Test.txt",
"Size": "27 B",
"Path": "D:\\Projects\\Test.txt"
},
...
],
"Children": [
{
"Name": "SubProjects",
"Files": [
{
"Name": "SubTest.txt",
"Size": "2 B",
"Path": "D:\\Projects\\SubProjects\\SubTest.txt"
},
...
],
"Children": [
....
]
},
{
"Name": "SubProjects3",
"Files": [],
"Children": []
},
...
]}
现在我在嵌套节点中添加信息时遇到问题。 我试图通过搜索新的密钥“级别”来解决此问题,并试图找到此密钥等于我需要的级别,但仍然无法正常工作。 我的代码:
#include <iostream>
#include <conio.h>
#include <nlohmann/json.hpp>
#include <fstream>
#include <iomanip>
#include <string>
#include <sstream>
#include <experimental/filesystem>
using namespace std;
using json = nlohmann::json;
namespace fs = std::experimental::filesystem;
void DisplayFileInfo(const fs::v1::directory_entry& entry, fs::v1::path& filename , json &j_main,int level)
{
json j_file;
j_file["Name"] = filename.string();
j_file["Size"] = fs::file_size(entry);
j_file["Path"] = fs::absolute(filename).string();
for ( auto& obj : j_main) {
if (obj["Level"] == level) {
obj["Files"].push_back(j_file);
}
}
}
void DisplayFolderInfo(const fs::v1::directory_entry& entry, fs::v1::path& filename, json &j_main, int level)
{
json j_folder;
j_folder["Level"] = level+1;
j_folder["Name"] = filename.string();
j_folder["Files"] = json::array({});
j_folder["Children"] = json::array({});
for (auto& obj : j_main)
{
if (obj["Level"] == level) {
obj["Children"].push_back(j_folder);
}
}
void DisplayDirectoryTree(const fs::path& pathToShow, int level, json &j_main)
{
if (fs::exists(pathToShow) && fs::is_directory(pathToShow))
{
for (const auto& entry : fs::directory_iterator(pathToShow))
{
auto filename = entry.path().filename();
if (fs::is_directory(entry.status()))
{
DisplayFolderInfo(entry, filename,j_main,level);
level++;
DisplayDirectoryTree(entry,level,j_main);
}
else if (fs::is_regular_file(entry.status()))
DisplayFileInfo(entry, filename,j_main,level);
}
}
}
int main()
{
char folder_path[255];
cout << "Please input name of folder with full path: " << endl;
cin >> folder_path;
json j_main;
j_main["Level"] = 0;
j_main["Children"] = json::array({});
j_main["Files"] = json::array({});
const fs::path pathToShow=folder_path;
DisplayDirectoryTree(pathToShow, 0,j_main);
ofstream o("file.json");
o << setw(4) << j_main << endl;
_getch();
return 0;
}
答案 0 :(得分:0)
您无需搜索任何内容,只需传递子树即可代替j_main
。
或者不传递任何东西,逻辑非常简单以适合单个函数:
void DisplayDirectoryTree(const fs::path & pathToShow, json& root, int level = 0)
{
std::vector<json> files, children;
root["Level"] = level;
for (const auto& entry : fs::directory_iterator(pathToShow))
{
json j_entry;
j_entry["Name"] = entry.path().filename().u8string();
if (fs::is_directory(entry.status()))
{
DisplayDirectoryTree(entry.path(), j_entry, level + 1);
children.emplace_back(std::move(j_entry));
}
else
{
j_entry["Size"] = fs::file_size(entry);
j_entry["Path"] = fs::absolute(entry.path()).u8string();
files.emplace_back(std::move(j_entry));
}
}
root["Files"] = files;
root["Children"] = children;
}
int main()
{
std::string folder_path;
cout << "Please input name of folder with full path: " << endl;
cin >> folder_path;
json j_main;
DisplayDirectoryTree(fs::path(folder_path), j_main);
ofstream o("file.json");
o << j_main.dump(4) << endl;
o.close();
return 0;
}
还请注意,您的level++
逻辑是错误的-您无需在循环中递增它,而需要将level + 1
传递到递归调用中。