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

时间:2014-06-16 23:22:47   收藏:0   阅读:557
//获取一个可写入的全路径
	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;


以下是生成效果:

bubuko.com,布布扣

 

bubuko.com,布布扣

cocos2d-x 3.0 交流群:  367086379

 367086379

 

 

 

cocos2d-x 3.0 rapidjson 的写入操作应该注意的细节,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!