void x6()
{
rapidjson::Document document;
std::string str = "{}"; // 这个是必须的,且不能为"",否则Parse出错
(str.c_str());
// 新增成员count
// AddMember第一个参数可以为字符串常,如“str”,不能为“const char*”和“std::string”,
// 如果使用“const char*”,则需要使用StringRefType转换:StringRefType(str.c_str())
("count", 3, ());
// 新增数组成员
rapidjson::Value array(rapidjson::kArrayType);
rapidjson::Value object(rapidjson::kObjectType); // 数组成员
("id", 1, ());
("name", "zhangsan", ());
// 如果数组添加无名字的成员,定义Value时应当改成相应的类型,如:
//rapidjson::Value value(rapidjson::kStringType);
//rapidjson::Value value(rapidjson::kNumberType);
//rapidjson::Value value(rapidjson::kFalseType);
//rapidjson::Value value(rapidjson::kTrueType);
//(value, ());
//效果将是这样:'array':[1,2,3,4,5]
// 注意下面用法编译不过:
//std::string str1 = "hello";
//("name", str1.c_str(), ());
//const char* str2 = "hello";
//("name", str2, ());
//
// 下面这样可以:
//("name", "hello", ());
//const char str3[] = "hello";
//("name", str3, ());
//
//std::string str4 = "#####";
//rapidjson::Value v(str4.c_str(), ());
//("x", v, ());
// 上面两行也可以写在一行:
//("x", rapidjson::Value(str4.c_str(), ()).Move(), ());
// 添加到数组中
(object, ());
// 添加到document中
("names", array, ());
// 转成字符串输出
rapidjson::StringBuffer buffer1;
rapidjson::Writer<rapidjson::StringBuffer> writer1(buffer1);
(writer1);
printf("%s\n", ());
// 修改值
rapidjson::Value& count_json = document["count"];
count_json.SetInt(9);
// 再次输出
rapidjson::StringBuffer buffer2;
rapidjson::Writer<rapidjson::StringBuffer> writer2(buffer2);
(writer2);
printf("%s\n", ());
}