在C+中创建json数组+; >我尝试在C++中动态创建JSON对象。我想添加一个时间戳,然后添加一个包含数据的数组
这就是我的json字符串的样子:在C+中创建json数组+; >我尝试在C++中动态创建JSON对象。我想添加一个时间戳,然后添加一个包含数据的数组,c++,arrays,json,casablanca,C++,Arrays,Json,Casablanca,这就是我的json字符串的样子: { "timestep": "2160.00", "vehicles": [ { "id": "35092_35092_353", "x": "6.988270", "y": "50.872139", "angle": "-20.812787", "type": "passenger_P_14_1",
{
"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++和IM使用CasabraCa(C++ +StestSDK)包。
所以我很难写出代码。我找不到任何有效的解决办法。我在维基上找到了这个
创建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包
帮忙真是太好了 有两种机制。如果您习惯于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;
你可以这样说:
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});
下面介绍如何使用
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);
std::矢量阵列车辆;
对于(int i=0;i<10;i++)
{
web::json::价值载体;
std::string vehicleID=“id\u prefix”+std::to\u string(i);
vehicle[“id”]=web::json::value::string(vehicleID);
车辆[“x”]=web::json::value::number(6.988270);
车辆[“y”]=web::json::value::number(50.872139);
排列车辆。推回(车辆);
}
web::json::value myJSON;
myJSON[“车辆”]=web::json::value::array(arrayVehicles);
以下是在卡萨布兰卡生成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"));
//...
如果您希望在收到的http_请求(在下面的情况下是
http_请求
)上使用该数组作为答案,您可以使用以下代码片段作为示例:
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);
json::value-answer;
自动数组=answer.array();
对于(size_t i=0;i
如果我把json::value::element\u向量e;在我的代码中,我得到一个错误元素_vector不是第二个解决方案的类json::value的一部分,它说arr3没有定义,这很有意义。。。arr3来自哪里?所以我得到了第二个解决方案。。但是对于多个条目,它会像。。。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”);你用的是什么版本?我在1.x上-我可以看到自从2.x以来,在数组和对象上发生了一些突破性的变化。最新的一个是2.0.1,我真的希望有一个有效的解决方案,因为json对象将被大量用于通信…关于你的第二条评论。。。你想解决什么问题?为了使代码更紧凑?但是有没有一种方法可以将车辆动态添加到数组中?因为我不知道会有多少辆车…@Hannes:你可以使用vector来动态创建和存储车辆。你能编辑它以便它动态查找车辆数量吗?我不知道“vehicles”的总数,所以我需要动态创建for循环的大小。什么是GenFile.GetNumberOfCurves()
,它如何连接到WhateVerray
@Jolley71717抱歉,这个代码片段是从我的大学项目中获得的(有一些更改)。您可以指定任何限制而不是GenFile.GetNumberOfCurves()
,并且任何数值都不是WhatEverArray[i]。whatever()