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<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;
}


版权声明:本文博主原创文章,博客,未经同意不得转载。






本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/4884790.html,如需转载请自行联系原作者


相关文章
|
17天前
|
JSON 前端开发 Java
Json格式数据解析
Json格式数据解析
|
17天前
|
存储 JSON Apache
揭秘 Variant 数据类型:灵活应对半结构化数据,JSON查询提速超 8 倍,存储空间节省 65%
在最新发布的阿里云数据库 SelectDB 的内核 Apache Doris 2.1 新版本中,我们引入了全新的数据类型 Variant,对半结构化数据分析能力进行了全面增强。无需提前在表结构中定义具体的列,彻底改变了 Doris 过去基于 String、JSONB 等行存类型的存储和查询方式。
揭秘 Variant 数据类型:灵活应对半结构化数据,JSON查询提速超 8 倍,存储空间节省 65%
|
15天前
|
JSON NoSQL MongoDB
实时计算 Flink版产品使用合集之要将收集到的 MongoDB 数据映射成 JSON 对象而非按字段分割,该怎么操作
实时计算Flink版作为一种强大的流处理和批处理统一的计算框架,广泛应用于各种需要实时数据处理和分析的场景。实时计算Flink版通常结合SQL接口、DataStream API、以及与上下游数据源和存储系统的丰富连接器,提供了一套全面的解决方案,以应对各种实时计算需求。其低延迟、高吞吐、容错性强的特点,使其成为众多企业和组织实时数据处理首选的技术平台。以下是实时计算Flink版的一些典型使用合集。
|
17天前
|
存储 JSON 数据处理
从JSON数据到Pandas DataFrame:如何解析出所需字段
从JSON数据到Pandas DataFrame:如何解析出所需字段
31 1
|
5天前
|
SQL 存储 JSON
Hive 解析 JSON 字符串数据的实现方式
Hive 提供 `get_json_object` 函数解析 JSON 字符串,如 `{&quot;database&quot;:&quot;maxwell&quot;}`。`path` 参数使用 `$`、`.`、`[]` 和 `*` 来提取数据。示例中展示了如何解析复杂 JSON 并存储到表中。此外,Hive 3.0.0及以上版本内置 `JsonSerDe` 支持直接处理 JSON 文件,无需手动解析。创建表时指定 `JsonSerDe` 序列化器,并在 HDFS 上存放 JSON 文件,可以直接查询字段内容,方便快捷。
|
17天前
|
XML JSON API
转Android上基于JSON的数据交互应用
转Android上基于JSON的数据交互应用
14 1
|
17天前
|
JSON JavaScript Java
从前端Vue到后端Spring Boot:接收JSON数据的正确姿势
从前端Vue到后端Spring Boot:接收JSON数据的正确姿势
30 0
|
17天前
|
JSON 数据格式 Python
Python标准库中包含了json模块,可以帮助你轻松处理JSON数据
【4月更文挑战第30天】Python的json模块简化了JSON数据与Python对象之间的转换。使用`json.dumps()`可将字典转为JSON字符串,如`{&quot;name&quot;: &quot;John&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;New York&quot;}`,而`json.loads()`则能将JSON字符串转回字典。通过`json.load()`从文件读取JSON数据,`json.dump()`则用于将数据写入文件。
18 1
|
17天前
|
JSON 数据格式 Python
Python处理JSON数据
【4月更文挑战第30天】该内容介绍了Python处理JSON数据的三个方法:1)使用`json.loads()`尝试解析字符串以验证其是否为有效JSON,通过捕获`JSONDecodeError`异常判断有效性;2)通过`json.dumps()`的`indent`参数格式化输出JSON数据,使其更易读;3)处理JSON中的日期,利用`dateutil`库将日期转换为字符串进行序列化和反序列化。
25 4
|
17天前
|
存储 JSON 数据处理