Han*_*nes 12 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"
}
]
}
Run Code Online (Sandbox Code Playgroud)
我是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"));
Run Code Online (Sandbox Code Playgroud)
这对我有用.但是我如何创建一个数组呢?
我尝试了几件事,但没有任何效果.也许是一个更好的包裹?但据我所知,它是json和http的官方micorosft包.
帮助会非常好!
有两种机制。如果您习惯于使用标准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);
Run Code Online (Sandbox Code Playgroud)
而且,如果您希望外观更整洁,并且可以接受效率较低的编译结果:
json::value arr;
arr[0] = json::value(false);
arr[1] = json::value(U("hello"));
Run Code Online (Sandbox Code Playgroud)
从您的消息中,您尝试了很多东西。如果您尝试过类似的机制,但它们无法正常工作,请给我们提供一个示例程序,该程序可以消除故障,然后我们将对其进行修复。
在上面的文件中获取基本结构:
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;
Run Code Online (Sandbox Code Playgroud)
以下是使用动态创建数组的方法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);
Run Code Online (Sandbox Code Playgroud)
你可以这样说:
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});
Run Code Online (Sandbox Code Playgroud)
这是在卡萨布兰卡生成 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"));
//...
Run Code Online (Sandbox Code Playgroud)