C++ 如何在JSON中创建文件层次结构表示
我是第一次使用,我必须创建如下文件层次结构:C++ 如何在JSON中创建文件层次结构表示,c++,nlohmann-json,C++,Nlohmann Json,我是第一次使用,我必须创建如下文件层次结构: { "Files": [ { "Name": "Test.txt", "Size": "27 B", "Path": "D:\\Projects\\Test.txt" }, ... ], "Children": [ { "Name": "SubProjects", "Files": [ { "Name"
{
"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": []
},
...
]}
现在我在嵌套节点中添加信息时遇到了一个问题。
我试图通过搜索新的键“level”来解决这个问题,并试图找到这个键在哪里等于我需要的level,但它仍然不起作用。
我的代码:
#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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
使用json=nlohmann::json;
命名空间fs=std::实验::文件系统;
void DisplayFileInfo(常量fs::v1::directory\u entry&entry,fs::v1::path&filename,json&j\u main,int级别)
{
json j_文件;
j_file[“Name”]=filename.string();
j_file[“Size”]=fs::file_Size(条目);
j_file[“Path”]=fs::absolute(filename).string();
用于(自动和车载:j_main){
如果(obj[“级别”]==级别){
obj[“文件”]。向后推(j文件);
}
}
}
void DisplayFolderInfo(常量fs::v1::directory\u entry&entry,fs::v1::path&filename,json&j\u main,int级别)
{
json j_文件夹;
j_文件夹[“级别”]=级别+1;
j_文件夹[“Name”]=filename.string();
j_folder[“Files”]=json::array({});
j_folder[“Children”]=json::array({});
用于(自动和车载:j_main)
{
如果(obj[“级别”]==级别){
obj[“Children”]。向后推(j_文件夹);
}
}
void DisplayDirectoryTree(常量fs::path&pathToShow,int级别,json&j_main)
{
if(fs::exists(pathToShow)&&fs::is_目录(pathToShow))
{
for(const auto&entry:fs::directory_迭代器(pathToShow))
{
自动文件名=entry.path().filename();
if(fs::is_目录(entry.status()))
{
DisplayFolderInfo(条目、文件名、j_main、级别);
级别++;
DisplayDirectoryTree(条目、级别、j_主目录);
}
else if(fs::is_regular_文件(entry.status()))
DisplayFileInfo(条目、文件名、j_main、级别);
}
}
}
int main()
{
char文件夹_路径[255];
cout文件夹路径;
朱梅因;
j_main[“Level”]=0;
j_main[“Children”]=json::array({});
j_main[“Files”]=json::array({});
常量fs::path pathToShow=文件夹路径;
DisplayDirectoryTree(路径显示,0,j_main);
流o(“file.json”);
o您不需要搜索任何内容,只需传递子树即可,而不是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;
}
void DisplayDirectoryTree(常量fs::path&pathtshow,json&root,int-level=0)
{
向量文件,子文件;
根[“级别”]=级别;
for(const auto&entry:fs::directory_迭代器(pathToShow))
{
json j_条目;
j_entry[“Name”]=entry.path().filename().u8string();
if(fs::is_目录(entry.status()))
{
DisplayDirectoryTree(entry.path(),j_entry,级别+1);
儿童。安置(标准::移动(j_条目));
}
其他的
{
j_entry[“Size”]=fs::file_Size(条目);
j_entry[“Path”]=fs::absolute(entry.Path()).u8string();
文件。向后放置(std::move(j_entry));
}
}
根[“文件”]=文件;
根[“Children”]=子对象;
}
int main()
{
std::字符串文件夹路径;
cout文件夹路径;
朱梅因;
DisplayDirectoryTree(fs::path(文件夹路径),j_main);
流o(“file.json”);
o您不需要搜索任何内容,只需传递子树即可,而不是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;
}
void DisplayDirectoryTree(常量fs::path&pathtshow,json&root,int-level=0)
{
向量文件,子文件;
根[“级别”]=级别;
for(const auto&entry:fs::directory_迭代器(pathToShow))
{
json j_条目;
j_entry[“Name”]=entry.path().filename().u8string();
if(fs::is_目录(entry.status()))
{
DisplayDirectoryTree(entry.path(),j_entry,级别+1);
儿童。安置(标准::移动(j_条目));
}
其他的
{
j_entry[“Size”]=fs::file_Size(条目);
j_entry[“Path”]=fs::absolute(entry.Path()).u8string();
文件。向后放置(std::move(j_entry));
}
}
根[“文件”]=文件;
根[“Children”]=子对象;
}
int main()
{
std::字符串文件夹路径;
cout文件夹路径;
朱梅因;
DisplayDirectoryTree(fs::path(文件夹路径),j_main);
流o(“file.json”);
o