
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
总结以上是内存溢出为你收集整理的cocos2dx 3.X 中 json 文件生成与读取全部内容,希望文章能够帮你解决cocos2dx 3.X 中 json 文件生成与读取所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)