微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

cocos2dx 3.X 中 json 文件生成与读取

Cocos2d-x 3.0 加入了rapidjson库用于json解析。位于项目的cocos2d/external/json下。

rapidjson 是一个不需要包含 .lib 和 .dll 即可运行的可见代码库。项目 wiki 见这里。下面通过两个实例来深入了解它在 cocos2dx 中的用法。

注:CCLOG() 函数需要在 DEBUG 模式下才有作用。

生成JSON文件并保存

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

USING_NS_CC;

int main()
{
	//*** 生成 json 文件,存储在 getWritablePath 文件夹下 ***
    rapidjson::Document writedoc;
	writedoc.SetObject();
	rapidjson::Document::AllocatorType& allocator = writedoc.GetAllocator();
	rapidjson::Value array(rapidjson::kArrayType);
	rapidjson::Value object(rapidjson::kObjectType);
	
	// json object 格式添加 “名称/值” 对
	object.AddMember("inttag",1,allocator);
	object.AddMember("doubletag",1.0,allocator);
	object.AddMember("booltag",true,allocator);
	object.AddMember("hellotag","helloworld",allocator);
	
	// json 加入数组
	array.PushBack(object,allocator);
	
	// json object 格式添加 “名称/值” 对
	writedoc.AddMember("json","json string",allocator);
	writedoc.AddMember("array",array,allocator);
 
	StringBuffer buffer;
	rapidjson::Writer<StringBuffer> writer(buffer);
	writedoc.Accept(writer);

	auto path = FileUtils::getInstance()->getWritablePath();
	path.append("myhero.json");
	FILE* file = fopen(path.c_str(),"wb");
	if(file)
	{
		fputs(buffer.GetString(),file);
		fclose(file);
	}
	CCLOG("%s",buffer.GetString());

    return 0;
}


我是用 VS2012 编译的,最终生成的json文件位于 \proj.win32\Debug.win32 文件夹下。打开内容如下:

{"json":"json string","array":[{"inttag":1,"doubletag":1,"booltag":true,"hellotag":"helloworld"}]}

读取JSON文件并显示

rapidjson 需要根据原 json 格式单独编写解析方法,因此根据以上生成方法,解析方法应该为:

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

USING_NS_CC;

int main()
{
	auto path = FileUtils::getInstance()->getWritablePath();
	path.append("myhero.json");

	//*** 读取 json 文件 ***
	rapidjson::Document readdoc;
	bool bRet = false;
	ssize_t size = 0;
	std::string load_str;

	// getFileData 如果不指定,读取根目录是 Resource 文件夹
	unsigned char* titlech = FileUtils::getInstance()->getFileData(path,"r",&size);
	load_str = std::string((const char*)titlech,size);

	//load_str = cocos2d::FileUtils::getInstance()->getStringFromFile("..\\myhero.json");
	readdoc.Parse<0>(load_str.c_str());	
	if(readdoc.HasParseError())
	{
		CCLOG("GetParseError%s\n",readdoc.GetParseError());
	}

	if(!readdoc.IsObject())
		return 0;

	rapidjson::Value& _json = readdoc["json"];
	const char* ch = _json.GetString();
	cocos2d::log(ch);
	cocos2d::log(_json.GetString());

	rapidjson::Value& _array = readdoc["array"];
	if(_array.IsArray())
	{
		CCLOG("test");
		for(int i=0; i<_array.Capacity(); i++)
		{
			//CCLOG("%d",i);
			rapidjson::Value& arraydoc = _array[i];
			if(arraydoc.HasMember("inttag"))
			{
				int _inttag = arraydoc["inttag"].GetInt();
				CCLOG("%d",_inttag);
			}
			if(arraydoc.HasMember("doubletag"))
			{
				double _doubletag = arraydoc["doubletag"].GetDouble();
				CCLOG("%lf",_doubletag);
			}
			if(arraydoc.HasMember("booltag"))
			{
				bool _booltag = arraydoc["booltag"].GetBool();
				CCLOG("%d",_booltag);
			}
			if(arraydoc.HasMember("hellotag"))
			{
				const char* _hellotag = arraydoc["hellotag"].GetString();
				CCLOG("%s",_hellotag);
			}
		}
	}

    return 0;
}
CCLOG 的最终显示为:

json string json string test 1 1.000000 1 helloworld

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐