Create a json array in C ++

So, I am trying to create a json Object in C ++ dynamically. I want to add a timestamp, and then an array with data included.

So here is what my json String will 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 completely new to C ++ and im using the Casablanca package (C ++ REST SDK). Therefore, it is very difficult for them to create code. And I can not 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 it works for me. But how do I create an array?

I tried a few things, but nothing worked. Maybe the best package? But as I understand it its official micorosft package for json and http.

Help will be very nice!

+7
c ++ json arrays casablanca
source share
5 answers

There are 2 mechanisms. If you are used to std C ++ libraries, this should look familiar. The 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 tried a bunch of things. If you tried mechanisms like these, but they didnโ€™t work, give us an example of a program that shows failure, and we will have a crack.

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; 
+2
source share

Here's how to create an array dynamically using vector . Suppose you have 10 cars.

 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); 
+6
source share

You can do the following:

 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}); 
+5
source share

Here is another way to create 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")); //... 
+3
source share

If you want to use an array as a response to the received http_request (in the case below), you can use the following code fragment 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); 
0
source share

All Articles