15

I'm trying to create a JSON object in C++ dynamically. I want to add a timestamp and then an array with the data included.

This is 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"
        }
    ]
}

I'm totally new to C++ and I'm using the Casablanca (C++ REST SDK) package. I'm 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 there's a better package? But as far as I understood it's an official Microsoft package for JSON and HTTP.

6 Answers 6

13

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;
Sign up to request clarification or add additional context in comments.

7 Comments

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??
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"));
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.
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...
regarding your second comment ... what are you trying to solve? To make the code more compact?
|
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);

1 Comment

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.
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});

2 Comments

but is there a method to add the vehicles dynamically to the array? because i dont know how many vehicles it will be ...
@Hannes: you can use vector to create and store vehicles dynamically.
5

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"));
//...

Comments

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);

2 Comments

What is GenFile.GetNumberOfCurves() and how does it connect to WhatEverArray
@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()
-1

The following sample json C++ array of strings works for me.

const char * const line_items_items =
"[\
    {\
        \"commodity_code\": \"44121903\",\
        \"description\": \"Miscellaneous goods\",\
        \"upc\": \"65100004327\",\
        \"quantity\": \"2\",\
        \"unit_of_measurement\": \"M62\",\
        \"unit_cost\": \"23.09\",\
        \"discount_amount\": \"10.03\",\
        \"total_amount\": \"50.03\",\
        \"tax_amount\": \"10.05\",\
        \"extended_amount\": \"76.04\",\
        \"debit_or_credit_indicator\": \"credit\",\
        \"net_or_gross_indicator\": \"net\"\
    },\
    {\
        \"commodity_code\": \"44121809\",\
        \"description\": \"Miscellaneous goods\",\
        \"upc\": \"65100007654\",\
        \"quantity\": \"4\",\
        \"unit_of_measurement\": \"M66\",\
        \"unit_cost\": \"35.09\",\
        \"discount_amount\": \"5.06\",\
        \"total_amount\": \"0.53\",\
        \"tax_amount\": \"8.07\",\
        \"extended_amount\": \"96.12\",\
        \"debit_or_credit_indicator\": \"debit\",\
        \"net_or_gross_indicator\": \"gross\"\
    }\
]";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.