13

So im trying to create a json Object in c++ dynamically. I want to add a timestamp and then an array with the data included.

So thats what my json String would look like :

{
    "timestep": "2160.00",
    "vehicles": [
        {
            "id": "35092_35092_353",
            "x": "6.988270",
            "y": "50.872139",
            "angle": "-20.812787",
            "type": "passenger_P_14_1",
            "speed": "0.000000",
            "pos": "4.600000",
            "lane": "4.600000",
            "slope": "4.600000"
        },
        {
            "id": "35092_35092_353",
            "x": "6.988270",
            "y": "50.872139",
            "angle": "-20.812787",
            "type": "passenger_P_14_1",
            "speed": "0.000000",
            "pos": "4.600000",
            "lane": "4.600000",
            "slope": "4.600000"
        },
        {
            "id": "35092_35092_353",
            "x": "6.988270",
            "y": "50.872139",
            "angle": "-20.812787",
            "type": "passenger_P_14_1",
            "speed": "0.000000",
            "pos": "4.600000",
            "lane": "4.600000",
            "slope": "4.600000"
        }
    ]
}

Im totally new to C++ and im using the Casablanca ( C++ REST SDK) package. So im having a really hard time producing the code. And i cant find any working solutions. I found this on the wiki

Create a JSON object:

json::value obj;
obj[L"key1"] = json::value::boolean(false);
obj[L"key2"] = json::value::number(44);
obj[L"key3"] = json::value::number(43.6);
obj[L"key4"] = json::value::string(U("str"));

and that works for me. But how do i create an array?

i tried several things but nothing worked. Maybe theres a better package? But as far as i understood its an official micorosft package for json and http.

Help would be really nice!

Hannes
  • 422
  • 1
  • 6
  • 23

5 Answers5

7

There are 2 mechanisms. If you are used to std c++ libraries, this should look familiar. Element vector is derived from std::vector.

json::value::element_vector e;
// the first item in the pair is the array index, the second the value
e.push_back(std::make_pair(json::value(0), json::value(false)));
e.push_back(std::make_pair(json::value(1), json::value::string(U("hello"))));
json::value arr(e);

And, if you prefer a cleaner look, and can accept a less efficient compiled result:

json::value arr;
arr[0] = json::value(false);
arr[1] = json::value(U("hello"));

From your message you have tried a bunch of stuff. If you have tried mechanisms like these but they didn't work, give us a sample program that demontrates the failure and we'll have a crack at it.

To get the basic structure in your file above:

json::value vehicles;
vehicles[0] = // 1st vehicle object
vehicles[1] = // 2nd vehicle object
// etc
json::value root;
root[L"timestep"] = json::number(2160.0);
root[L"vehicles"] = vehicles;
FatalFlaw
  • 937
  • 9
  • 16
  • if i put json::value::element_vector e; in my code i get an error element_vector is not part of the class json::value for the second solution it says arr3 is not defined which makes sense... where does arr3 come from?? – Hannes Apr 17 '14 at 12:49
  • So i got the second solution .. but for multiple entries would it be like ... json::value arr; arr[0] = json::value("id"); arr[1] = json::value(U("35092_35092_353")); arr[2] = json::value("x"); arr[3] = json::value(U("6.988270")); arr[4] = json::value("y"); arr[5] = json::value(U("50.872139")); – Hannes Apr 17 '14 at 12:59
  • What version are you using? I am on 1.x - I can see there have been some breaking changes since 2.x on arrays and objects. – FatalFlaw Apr 17 '14 at 13:01
  • The newest one 2.0.1, and i really would like to have an efficient solution because that json object will be used heavily for communication... – Hannes Apr 17 '14 at 13:02
  • regarding your second comment ... what are you trying to solve? To make the code more compact? – FatalFlaw Apr 17 '14 at 13:02
  • No like i posted above i have multiple array entries in my json string not only one. So i was wondering how that would work then – Hannes Apr 17 '14 at 13:04
  • Added to answer above. – FatalFlaw Apr 17 '14 at 13:39
7

Here's how to create an array dynamically using vector. Assume that you have 10 vehicles to add.

std::vector<web::json::value> arrayVehicles;
for(int i = 0; i < 10; i++)
{
    web::json::value vehicle;
    std::string vehicleID = "id_prefix_" + std::to_string(i);
    vehicle["id"] = web::json::value::string(vehicleID);
    vehicle["x"] = web::json::value::number(6.988270);
    vehicle["y"] = web::json::value::number(50.872139);

    arrayVehicles.push_back(vehicle);
}

web::json::value myJSON;
myJSON["vehicles"] = web::json::value::array(arrayVehicles);
Thanh-Nhon Nguyen
  • 3,026
  • 3
  • 25
  • 37
  • Can you edit this so that it finds the number of vehicles dynamically? I won't know the total number of "vehicles", so I'll need to dynamically create the size of the for loop. – Jolley71717 Dec 19 '17 at 00:12
5

You could put it like this:

json::value vehicle1;
vehicle1[L"id"] = json::value::string(L"35092_35092_353");
vehicle1[L"x"] = json::value::number(6.988270);
vehicle1[L"y"] = json::value::number(50.872139);

json::value vehicle2;
vehicle2[L"id"] = json::value::string(L"35092_35092_353");
vehicle2[L"x"] = json::value::number(1.23456);
vehicle2[L"y"] = json::value::number(6.78901);

json::value vehicles;
vehicles[L"timestamp"] = json::value::number(2160);
vehicles[L"vehicles"] = json::value::array({vehicle1, vehicle2});
Marius Bancila
  • 15,287
  • 7
  • 44
  • 84
4

Here is another method to produce a json array in Casablanca:

int size = 3;
web::json::value yourJson;
yourJson[U("vehicles")] = web::json::value::array(size);

yourJson[U("vehicles")].as_array()[0] = web::json::value(U("some entry"));
yourJson[U("vehicles")].as_array()[1] = web::json::value(U("another entry"));
//...
davidhigh
  • 12,239
  • 1
  • 34
  • 64
0

If you wish to use the array as an answer on a received http_request (in case below it's a http_request request), you are free to use the following snippet of code as an example:

    json::value answer;
    auto array = answer.array();

    for (size_t i = 0; i < GenFile.GetNumberOfCurves(); i++)
    {
        web::json::value vehicle;

        vehicle[L"smth"] = web::json::value::number(WhatEverArray[i].whatever());

        array[i] = vehicle;
    }

    request.reply(status_codes::OK, array);
Dmitry
  • 206
  • 2
  • 4
  • What is `GenFile.GetNumberOfCurves()` and how does it connect to `WhatEverArray` – Jolley71717 Dec 20 '17 at 23:30
  • @Jolley71717 Sorry, this code snippet was obtained from my University project (w/ some changes). You can specify any limit instead of `GenFile.GetNumberOfCurves()` and any numeric value isntead of `WhatEverArray[i].whatever()` – Dmitry Jan 29 '18 at 09:33