標籤:cocos2d-x rapidjson document json 解析
2dx3.0下JSON解析庫官方已經整合好了,我們引用就OK。
JSON檔案hello.json內容
{"pets":["dog","cat"],"stuInfo":{"stuAge":"23","stuName":"zhangsan","birthday":"1990-01-12"},"username":"tomsfff","other":[true,30]}
.h標頭檔
#include "cocos-ext.h"#include "tinyxml2/tinyxml2.h"
.cpp
//擷取檔案路徑 const char* file_path = FileUtils::getInstance()->fullPathForFilename("hello.json").c_str(); log("external file path = %s",file_path); rapidjson::Document d1; std::string contentStr = FileUtils::getInstance()->getStringFromFile(file_path); d1.Parse<0>(contentStr.c_str()); //列印JSon檔案的內容 printf("%s\n",contentStr.c_str()); //擷取JSon中數組的方法 const rapidjson::Value& v = d1["pets"]; if (v.IsArray()) { //這裡一定要注意 變數i 一定要是 unsigned int 不然會報錯 for (unsigned int i = 0; i< v.Size(); ++i) { const rapidjson::Value &val = v[i]; log("%s",val.GetString()); } }else { const rapidjson::Value& val = v["stuAge"]; log("val.GetString() = %s",val.GetString()); }