在c++中创建一个json数组

时间:2021-12-08 21:21: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.

因此我尝试在c++中动态创建一个json对象。我想添加一个时间戳和包含数据的数组。

So thats what my json String would look like :

这就是json字符串的样子

{
    "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

我对c++和使用Casablanca (c++ REST SDK)包完全陌生。所以我很难写出代码。我找不到任何有效的解决办法。我在维基上发现了这个。

Create a JSON object:

创建一个JSON对象:

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.

我试了好几样东西,但都没有用。也许有更好的方案?但据我所知,这是一个针对json和http的官方micorosft包。

Help would be really nice!

帮助真是太好了!

5 个解决方案

#1


2  

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

有两个机制。如果您习惯了std c++库,这应该看起来很熟悉。元素向量来自std::向量。

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;

#2


6  

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

下面介绍如何使用vector动态创建数组。假设你要增加10辆车。

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

#3


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

#4


3  

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

下面是在Casablanca中生成json数组的另一种方法:

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

#5


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:

如果您希望使用数组作为接收到的http_request的答案(如果下面是http_request请求),您可以*地使用以下代码片段作为示例:

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

#1


2  

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

有两个机制。如果您习惯了std c++库,这应该看起来很熟悉。元素向量来自std::向量。

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;

#2


6  

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

下面介绍如何使用vector动态创建数组。假设你要增加10辆车。

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

#3


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

#4


3  

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

下面是在Casablanca中生成json数组的另一种方法:

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

#5


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:

如果您希望使用数组作为接收到的http_request的答案(如果下面是http_request请求),您可以*地使用以下代码片段作为示例:

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