jsoncpp 序列化和反序列化的demo


/*
{
	"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");

}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值