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 writer(buffer);
document.accept(writer);
//log("%s\n", buffer.getstring());