/*
{
"width" : 640,
"height": 480,
"Base64": "hello world and this is a Base64 image ",
"feature":[0.22, 0.38, 0.68, .......]
}
*/
void testJson()
{
// 将上面的结构数据,序列化为一个字符串
Json::FastWriter writer;
Json::Value value;
value["width"] = 640;
value["height"] = 480;
value["Base64"] = "hello world and this is a Base64 image ";
for (int i = 0; i < 512; i++)
{
value["feature"].append(1.234);
}
string jsonreq = writer.write(value);
std::cout << jsonreq << std::endl;
Json::Value val_array = value["feature"];
printf("value[feature].size = %d \n", val_array.size());
printf("val_array[1] = %f .....\n", val_array[1].asFloat());
// 将字符串反序列化为数据
string jsonresp(jsonreq.data(), jsonreq.size());
Json::Reader reader;
Json::Value resp;
if (!reader.parse(jsonresp, resp, false))
throw string("bad json format!\n");
int width = resp["width"].asInt();
int height = resp["height"].asInt();
std::string Base64 = resp["Base64"].asString();
Json::Value arrayObj = resp["feature"];
int mSize = arrayObj.size();
std::vector<float> mFeature;
for (int i = 0; i < mSize; i++)
{
mFeature.push_back(arrayObj[i].asFloat());
}
printf("over ..........\n");
}
jsoncpp 序列化和反序列化的demo
最新推荐文章于 2024-08-10 15:17:40 发布