您的位置:首页 > Web前端 > JavaScript

rapidjson 添加删除对象

2016-06-30 14:49 591 查看
rapidjson我就不介绍了,以后有机会在补上。

直接上代码

#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
#include <stdlib.h>
void func()
{
rapidjson::Document * ptr_doc = new rapidjson::Document();
ptr_doc->Parse("{}");
rapidjson::Document::AllocatorType&allocator = ptr_doc->GetAllocator();

rapidjson::Value current_gameplay(rapidjson::kObjectType);
rapidjson::Value current_gameplay_achievements(rapidjson::kObjectType);
ptr_doc->AddMember("current_gameplay", current_gameplay, allocator);//对象添加
ptr_doc->AddMember("current_gameplay_achievements", current_gameplay_achievements, allocator);

rapidjson::Value user(rapidjson::kObjectType);
if (!(*ptr_doc)["current_gameplay"].GetObject().HasMember("user"))//判断是否user对象
  {
    (*ptr_doc)["current_gameplay"].AddMember("user", user, allocator);//current_gameplay对象添加user对象
  }

rapidjson::Value strObject(rapidjson::kStringType);
strObject.SetString("123");
(*ptr_doc)["current_gameplay"]["user"].AddMember("id", strObject, allocator);//current_gameplay的user对象添加 id:"123";

(*ptr_doc)["current_gameplay"]["user"]["id"].SetString("321");//current_gameplay的user对象修改为 id:"321";
(*ptr_doc)["current_gameplay"]["user"].RemoveMember("id");//current_gameplay的user对象删除id项
rapidjson::StringBuffer  buffer;
rapidjson::Writer<rapidjson::StringBuffer>  writer(buffer);
ptr_doc->Accept(writer);
auto msgToString = buffer.GetString();
printf("info:%s", msgToString);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  rapidjson