Create a json array in C++

68,426

Solution 1

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;

Solution 2

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

Solution 3

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

Solution 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"));
//...
Share:
68,426
Hannes
Author by

Hannes

Updated on March 13, 2020

Comments

  • Hannes
    Hannes about 4 years

    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
    Hannes about 10 years
    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
    Hannes about 10 years
    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"));
  • FatalFlaw
    FatalFlaw about 10 years
    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.
  • Hannes
    Hannes about 10 years
    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...
  • FatalFlaw
    FatalFlaw about 10 years
    regarding your second comment ... what are you trying to solve? To make the code more compact?
  • Hannes
    Hannes about 10 years
    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
  • FatalFlaw
    FatalFlaw about 10 years
    Added to answer above.
  • Hannes
    Hannes about 10 years
    but is there a method to add the vehicles dynamically to the array? because i dont know how many vehicles it will be ...
  • Thanh-Nhon Nguyen
    Thanh-Nhon Nguyen almost 10 years
    @Hannes: you can use vector to create and store vehicles dynamically.
  • Jolley71717
    Jolley71717 over 6 years
    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
    Jolley71717 over 6 years
    What is GenFile.GetNumberOfCurves() and how does it connect to WhatEverArray
  • Dmitry
    Dmitry over 6 years
    @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()