I need to create the following structure but I am struggling to do this using json.
{
"ParentTree": [{
"Name": "Name3",
"children": [{
"Name": "Name2",
"children": [{
"Name": "Name1",
"children": [{
"Name": "Name0"
}]
}]
}]
}]
}
I have tried below but unable to get how to add name and children keys dynamically.
json jsonObj;
jsonObj["ParentTree"] = json::array();
for (auto index = 3; index >= 0; index--) {
jsonObj["ParentTree"].push_back(json::object());
}
Previously it was done using below way without using nlohmann json:
std::string PResult = "\"ParentTree\":[{";
for (int j = 3; j >= 0; j--)
{
std::string num = std::to_string(j);
PResult += "\"Name\":\"";
PResult += "Name";
PResult += num + "\",";
if (j == 0) break;
PResult += "\"children\":[{";
}
PResult.erase(PResult.length() - 1);
for (int j = 3; j >= 0; j--)
{
PResult += "}]";
}