vendredi 25 août 2017

creating nested json object in c++ using nlohmann json

I am working with http://ift.tt/1D7fWFV 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.

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;

}

Aucun commentaire:

Enregistrer un commentaire