RapidJSON解析和生成Json

RapidJSON学习

  • RapidJSON获取json数据键值对的key和value
    这里写图片描述
std::string output = "";

output += "Deck: \n";
// deck
for (unsigned j = 0; j < (document["data"])["deck"].Size(); j++) {
	// deck内遍历
	for (rapidjson::Value::ConstMemberIterator iter = ((document["data"])[i]["deck"])[j].MemberBegin(); iter != ((document["data"])["deck"])[j].MemberEnd(); iter++) {
		rapidjson::Value jKey;
		rapidjson::Value jValue;
		Document::AllocatorType allocator;
		jKey.CopyFrom(iter->name, allocator);
		jValue.CopyFrom(iter->value, allocator);
		if (jKey.IsString()) {
			std::string name = jKey.GetString();
			std::string value = cocos2d::Value(jValue.GetInt()).asString();
			output += "    " +  name + ':' + value + '\n';
		}
	}
	output += "    ---\n";
}
output += "---\n";

  • RapidJSON创建Json
    场景:用户输入用户名和密码,然后将信息组合生成json
	rapidjson::Document document;
	document.SetObject();
	rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
	
	//用户名
	rapidjson::Value username(StringRef(usernameInput->getString().c_str()));
	document.AddMember("username", username, allocator);
	
	//密码
	rapidjson::Value password(StringRef(passwordInput->getString().c_str()));
	document.AddMember("password", password, allocator);

	StringBuffer buffer;
	rapidjson::Writer<StringBuffer> writer(buffer);
	document.Accept(writer);
	//log("%s\n", buffer.GetString());
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值