cocos2dx 3.X 中 json 文件生成与读取
发布时间:2020-12-14 19:36:51 所属栏目:百科 来源:网络整理
导读:Cocos2d-x 3.0 加入了rapidjson库用于json解析。位于项目的cocos2d/external/json下。 rapidjson 是一个不需要包含 .lib 和 .dll 即可运行的可见代码库。项目 wiki 见这里。下面通过两个实例来深入了解它在 cocos2dx 中的用法。 注:CCLOG() 函数需要在 DEBU
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; }
{"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%sn",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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |