Cocos2d-x 3.0 加入了rapidjson库用于json解析。位于项目的cocos2d/external/json下。
rapidjson 是一个不需要包含 .lib 和 .dll 即可运行的可见代码库。项目 wiki 见这里。下面通过两个实例来深入了解它在 cocos2dx 中的用法。
注:CCLOG() 函数需要在 DEBUG 模式下才有作用。
- #include"CCStdC.h"
- #include"cocos2d.h"
- #include"json/document.h"
- #include"json/writer.h"
- #include"json/stringbuffer.h"
- usingnamespacerapidjson;
- USING_NS_CC;
- intmain()
- {
- rapidjson::Documentwritedoc;
- writedoc.SetObject();
- rapidjson::Document::AllocatorType&allocator=writedoc.GetAllocator();
- rapidjson::Valuearray(rapidjson::kArrayType);
- rapidjson::Valueobject(rapidjson::kObjectType);
- object.AddMember("inttag",1,allocator);
- object.AddMember("doubletag",1.0,allocator);
- object.AddMember("booltag",true,153); border-image: initial; list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; padding: 0px 3px 0px 10px !important;">object.AddMember("hellotag","helloworld",0); background-color: inherit;">//json加入数组
- array.PushBack(object,allocator);
- //jsonobject格式添加“名称/值”对
- writedoc.AddMember("json","jsonstring",85); line-height: 18px; padding: 0px 3px 0px 10px !important;">writedoc.AddMember("array",array,85); line-height: 18px; padding: 0px 3px 0px 10px !important;">StringBufferbuffer;
- rapidjson::Writer<StringBuffer>writer(buffer);
- writedoc.Accept(writer);
- autopath=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());
- return0;
- }
我是用 VS2012 编译的,最终生成的json文件位于 \proj.win32\Debug.win32 文件夹下。打开内容如下:
{"json":"json string","array":[{"inttag":1,"doubletag":1,"booltag":true,"hellotag":"helloworld"}]}
rapidjson 需要根据原 json 格式单独编写解析方法,因此根据以上生成方法,解析方法应该为:
- #include"CCStdC.h"
- #include"cocos2d.h"
- #include"json/document.h"
- #include"json/writer.h"
- #include"json/stringbuffer.h"
- usingnamespacerapidjson;
- USING_NS_CC;
- intmain()
- {
- autopath=FileUtils::getInstance()->getWritablePath();
- path.append("myhero.json");
- //***读取json文件***
- rapidjson::Documentreaddoc;
- boolbRet=false;
- ssize_tsize=0;
- std::stringload_str;
- //getFileData如果不指定,读取根目录是Resource文件夹
- unsignedchar*titlech=FileUtils::getInstance()->getFileData(path,"r",&size);
- load_str=std::string((constchar*)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())
- return0;
- rapidjson::Value&_json=readdoc["json"];
- constchar*ch=_json.GetString();
- cocos2d::log(ch);
- cocos2d::log(_json.GetString());
- rapidjson::Value&_array=readdoc["array"];
- if(_array.IsArray())
- CCLOG("test");
- for(inti=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();
- if(arraydoc.HasMember("hellotag"))
- constchar*_hellotag=arraydoc["hellotag"].GetString();
- }
CCLOG 的最终显示为:
json stringjson stringtest11.0000001helloworld