I am working with https://github.com/nlohmann/json and it works well. However I am finding difficulties to create the following json outout
{
"Id": 1,
"Child": [
{
"Id": 2
},
{
"Id": 3,
"Child": [
{
"Id" : 5
},
{
"Id" : 6
}
]
},
{
"Id": 4
}
]
}
Every node must have an id and an array ("Child" element). Any child can recursively continue to have Id or Child. The json above is just an example. What I want is to create a chain between father and children node using nlohmann json.
Numbers 1, 2, 3, .... are picked up randomly. We don't care for now about those values.
Any idea how to create it?
Code so far
#include <iostream>
#include <string>
#include <vector>
#include "json.hpp"
using json = nlohmann::json;
struct json_node_t {
int id;
std::vector<json_node_t> child;
};
int main( int argc, char** argv) {
json j;
for( int i = 0; i < 3; i++) {
json_node_t n;
n.id = i;
j["id"] = i;
if ( i < 2 ) {
j["child"].push_back(n);
}
}
return 0;
}