cocos2dx 3.2 自訂用rapidjson讀取json資料

來源:互聯網
上載者:User

cocos2dx 3.2 自訂用rapidjson讀取json資料
一、說明

在此處我只是簡單的定義了擷取string和Int類型,其它的換下資料類型就可以了。

二、標頭檔
class JsonReadUtils{public:static JsonReadUtils* getInstance();const std::string getStringFromeFile(const std::string &fileName);const std::string getStringFromeJson(const std::string jsonStr,const std::string key);const std::string getStringFromeJson(const std::string jsonStr,const std::string key1,const std::string key2);int getIntFromeJson(const std::string jsonStr,const std::string key);int getIntFromeJson(const std::string jsonStr,const std::string key1,const std::string key2);unsigned getSizeFromeJsonArr(const std::string jsonArr);JsonReadUtils();private:~JsonReadUtils(void);};


三、源檔案
#include "JsonUtils.h"#define RETURN_IF(cond,p)           if((cond)) return (p)static JsonReadUtils* mUtils = nullptr;JsonReadUtils::JsonReadUtils(){}JsonReadUtils::~JsonReadUtils(void){CC_SAFE_DELETE(mUtils);}JsonReadUtils* JsonReadUtils::getInstance(){if (NULL==mUtils){mUtils = new JsonReadUtils();}return mUtils;}const std::string JsonReadUtils::getStringFromeFile( const std::string &fileName){const std::string mstr = cocos2d::FileUtils::getInstance()->getStringFromFile(fileName);return mstr;}const std::string JsonReadUtils::getStringFromeJson( const std::string jsonStr,const std::string key ){rapidjson::Document _mDoc;std::string mstr = jsonStr;RETURN_IF(NULL==mstr.c_str()||!mstr.compare(""),"");_mDoc.Parse(mstr.c_str());RETURN_IF(_mDoc.HasParseError()||!_mDoc.IsObject()||!_mDoc.HasMember(key.c_str()),"");const rapidjson::Value &pArr = _mDoc[key.c_str()];RETURN_IF(!pArr.IsString(),"");const std::string mm = pArr.GetString();return mm;}const std::string JsonReadUtils::getStringFromeJson( const std::string jsonStr,const std::string key1,const std::string key2 ){rapidjson::Document _mDoc;std::string mstr = jsonStr;RETURN_IF(NULL==mstr.c_str()||!mstr.compare(""),"");_mDoc.Parse(mstr.c_str());RETURN_IF(_mDoc.HasParseError()||!_mDoc.IsObject()||!_mDoc.HasMember(key1.c_str()),"");const rapidjson::Value &pArr = _mDoc[key1.c_str()];RETURN_IF(!pArr.IsObject(),"");const rapidjson::Value &p = pArr[key2.c_str()];RETURN_IF(!p.IsString(),"");const std::string vvv =p.GetString();return vvv;}int JsonReadUtils::getIntFromeJson( const std::string jsonStr,const std::string key ){rapidjson::Document _mDoc;std::string mstr = jsonStr;RETURN_IF(NULL==mstr.c_str()||!mstr.compare(""),NULL);_mDoc.Parse(mstr.c_str());RETURN_IF(_mDoc.HasParseError()||!_mDoc.IsObject()||!_mDoc.HasMember(key.c_str()),NULL);const rapidjson::Value &pArr = _mDoc[key.c_str()];RETURN_IF(!pArr.IsInt(),NULL);int mm = pArr.GetInt();return mm;}int JsonReadUtils::getIntFromeJson( const std::string jsonStr,const std::string key1,const std::string key2 ){rapidjson::Document _mDoc;std::string mstr = jsonStr;RETURN_IF(NULL==mstr.c_str()||!mstr.compare(""),NULL);_mDoc.Parse(mstr.c_str());RETURN_IF(_mDoc.HasParseError()||!_mDoc.IsObject()||!_mDoc.HasMember(key1.c_str()),NULL);const rapidjson::Value &pArr = _mDoc[key1.c_str()];RETURN_IF(!pArr.IsObject(),NULL);const rapidjson::Value &p = pArr[key2.c_str()];RETURN_IF(!p.IsInt(),NULL);int vvv =p.GetInt();return vvv;}unsigned JsonReadUtils::getSizeFromeJsonArr( const std::string jsonArr ){rapidjson::Document _mDoc;std::string mstr = jsonArr;RETURN_IF(NULL==mstr.c_str()||!mstr.compare(""),NULL);_mDoc.Parse(mstr.c_str());RETURN_IF(_mDoc.HasParseError()||!_mDoc.IsObject(),NULL);const rapidjson::Value &mValue = _mDoc;RETURN_IF(!mValue.IsArray(),NULL);unsigned count = 0;unsigned mm = mValue.Capacity();return mm;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.