- c++实现json字符串
//头文件
#include "external/json/rapidjson.h"
#include "external/json/writer.h"
#include "external/json/document.h"
#include "external/json/stringbuffer.h"
//实现. 注意json语言的语法
void test() {
rapidjson::Document document; //根目录
document.SetObject();
rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
//rapidjson::Value 参数标示
//array 参数
//rapidjson::kArrayType 参数类型
rapidjson::Value array(rapidjson::kArrayType);
for (int i = 0; i < 3; ++i) {
rapidjson::Value object(rapidjson::kObjectType);
object.AddMember("name", "lx");
std::string address = "hhhhhhhh";
rapidjson::Value addressValue(rapidjson::kStringType);
addressValue.SetString(address.c_str(), address.size(), allocator);
object.AddMember("address", addressValue, allocator);
array.PushBack(object, allocator);
}
document.AddMember("array", array, allocator);
//绑定buffer
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
document.Accept(writer);
//获取生成的字符串
auto out = buffer.GetString();
cc.log("out=%s", out.c_str()