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

cocos2dx 3.2 自定义用rapidjson读取json数据,第1张

概述一、说明 在此处我只是简单的定义了获取string和Int类型,其它的换下数据类型就可以了。 二、头文件 class JsonReadUtils{public: static JsonReadUtils* getInstance(); const std::string getStringFromeFile(const std::string &fileName); const std: 一、说明

在此处我只是简单的定义了获取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 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 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,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;}
总结

以上是内存溢出为你收集整理的cocos2dx 3.2 自定义用rapidjson读取json数据全部内容,希望文章能够帮你解决cocos2dx 3.2 自定义用rapidjson读取json数据所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1008085.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-22
下一篇2022-05-22

发表评论

登录后才能评论

评论列表(0条)

    保存