构建json
Document doc; Document::AllocatorType &allocator=doc.GetAllocator(); //1.获取分配器 doc.SetObject(); //2,给doc对象赋值 Value pnameValue;pnameValue.SetString(StringRef(pname.c_str()));//创建"key":"value" Value tagsValue;tagsValue.SetObject();//创建{} tagsValue.AddMember("pname",pnameValue,allocator); doc.AddMember("tags",tagsValue,allocator); Value tempV; tempV.SetString(StringRef(value.c_str()));//std::string value("test"); doc.AddMember("value",tempV,allocator); //3,将doc对象的值写入字符串 StringBuffer buffer; //PrettyWriter<StringBuffer> writer(buffer); //PrettyWriter是格式化的json,如果是Writer则是换行空格压缩后的json Writer<StringBuffer> writer(buffer); doc.Accept(writer); return buffer.GetString();
效果:
{"tags": {"pname"="xxx"},"value":"123, 123, 456, 123"}
解析Json
略