cocos2dx 3.2 自定义用rapidjson读取json数据

来源:互联网 发布:网络教育统考时间 编辑:程序博客网 时间:2024/06/06 19:13

一、说明

在此处我只是简单的定义了获取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<rapidjson::kParseDefaultFlags>(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<rapidjson::kParseDefaultFlags>(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<rapidjson::kParseDefaultFlags>(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<rapidjson::kParseDefaultFlags>(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<rapidjson::kParseDefaultFlags>(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;}


1 0
原创粉丝点击