cocos2dx 3.X 中 json 檔案產生與讀取,cocos2dxjson
Cocos2d-x 3.0 加入了rapidjson庫用於json解析。位於項目的cocos2d/external/json下。
rapidjson 是一個不需要包含 .lib 和 .dll 即可啟動並執行可見程式碼程式庫。項目 wiki 見這裡。下面通過兩個執行個體來深入瞭解它在 cocos2dx 中的用法。
產生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