加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 19:19:13 所属栏目:百科 来源:网络整理
导读:一、说明 在此处我只是简单的定义了获取string和Int类型,其它的换下数据类型就可以了。 二、头文件 class JsonReadUtils{public:static JsonReadUtils* getInstance();const std::string getStringFromeFile(const std::string fileName);const std::string

一、说明

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读