jeudi 20 juin 2019

How to create a json object with hierarchical object and values

How to create a hierarchical JSON object and value structure using Casablanca/cpprestsdk JSON library?

I referred to the following links. But I could not figure out placing the JSON object in another JSON object

https://github.com/Microsoft/cpprestsdk/wiki/JSON https://microsoft.github.io/cpprestsdk/namespaceweb_1_1json.html

JSON Object:

{
    "employee" :
    {
        "name" : "vinkris",
        "age"  : "30"
    }
}

{
    struct employee emp;
    string object = U("{") + U("employee:") + U("{") ;
    object += U("name:") + emp.name + U(",") ;
    object += U("age:") + std::to_string(emp.age) + U("}") ;
    json::value = json::value::string(object)
}

is there any to simplify how the JSON body can be constructed?

For example:

{
    JsonObject object();
    JsonSubObjects subObject();
    subObjects.add("name", "vinkris");
    subObjects.add("age", "30");
    object.add("employee", subObject)
}

I am very new to both C++ and Casablanca library. Wondering if this is doable? It would be great to get some pointers on how to build something like this.

Aucun commentaire:

Enregistrer un commentaire