首页 > 代码库 > cocos2d-x 3.0 rapidjson 的写入操作应该注意的细节

cocos2d-x 3.0 rapidjson 的写入操作应该注意的细节

//获取一个可写入的全路径
	auto  path =FileUtils::getInstance()->getWritablePath();
	log("%s", path.c_str());
	//在这个路径下添加一个json文件
	path.append("myhero.json");

	rapidjson::Document document;
	document.SetObject();
	rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
	rapidjson::Value array(rapidjson::kArrayType);
		rapidjson::Value object(rapidjson::kObjectType);
		object.AddMember("id", 1, allocator);
		object.AddMember("name", "豹儿", allocator);
		object.AddMember("age", "3年", allocator);
		object.AddMember("low", true, allocator);
		array.PushBack(object, allocator);

	document.AddMember("propety", "PLAYER-TO", allocator);
	document.AddMember("player", array, allocator);

	rapidjson::StringBuffer  buffer;
	rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
	document.Accept(writer);
	FILE* file = fopen(path.c_str(), "wb");
	if (file)
	{
		fputs(buffer.GetString(), file);
		fclose(file);
	}


头文件要加上以下内容:

#include "json/document.h"
#include "json/writer.h"
#include "json/stringbuffer.h"
using namespace  rapidjson;


以下是生成效果:

 

cocos2d-x 3.0 交流群:  367086379

 367086379