用C ++创建一个json数组

时间:2014-04-17 12:03:22

标签: c++ arrays json casablanca

所以我试图动态地在c ++中创建一个json对象。我想添加一个时间戳,然后添加一个包含数据的数组。

那就是我的json String的样子:

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

我是C ++的新手,我正在使用Casablanca(C ++ REST SDK)包。 所以我很难生成代码。我找不到任何有效的解决方案。我在维基上发现了这个

创建一个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"));

这对我有用。但是我如何创建一个数组呢?

我尝试了几件事,但没有任何效果。也许是一个更好的包裹?但据我所知,它是json和http的官方micorosft包。

帮助真的很棒!

5 个答案:

答案 0 :(得分:6)

以下是使用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);

答案 1 :(得分:5)

你可以这样说:

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 :(得分:4)

有两种机制。如果您习惯使用std c ++库,这看起来应该很熟悉。元素向量派生自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);

并且,如果您更喜欢更清洁的外观,并且可以接受效率较低的编译结果:

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

从你的留言中你已经尝试了很多东西。如果你已经尝试过这样的机制但是它们没有用,那就给我们一个示例程序来消除失败,我们就会对它进行破解。

要获取上述文件中的基本结构:

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;

答案 3 :(得分:3)

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

答案 4 :(得分:0)

如果您希望将数组用作收到的http_request的答案(如果在它下面&#39; sa http_request 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);