目前,我正在做一个c++项目,使用CPPRESTSDK (也称为.卡萨布兰卡)。
在cpprestsdk库中已经实现了对int、double等值的序列化。
我现在想问,是否有任何方法可以在客户端将std::vector序列化为json::value,发出请求,然后在服务器端对其进行反序列化?
类似于:
http_client client(U("http://localhost"));
json::value jsonVector(std::vector);
make_task_request(client, methods::POST, jsonVector)提前感谢你所做的一切!
发布于 2017-05-29 02:12:38
向量序列化:
std::vector<int> someVector;
web::json::value json;
std::vector<value> array;
if (someVectory.size()) {
for (auto num : someVector) {
array.push_back(value(num));
}
json["yourKey"] = value::array(array);
}如果不需要将数组推入容器对象,那么只需使用将std::vector转换为数组的value::array(array)即可。
为了进行反序列化,假设您在array中有一个已知的数组,那么:
std::vector<int> intVector;
for (auto it=array.cbegin();it!=array.cend();++it) {
intVector.push_back(it->as_integer());
}https://stackoverflow.com/questions/43479692
复制相似问题