c++ json封装---解析
2016-04-14 19:46
543 查看
首先要有编译的json库
#pragma once
#include <json.h>
using namespace Json;
using namespace std;
class CParseJson
{
public:
CParseJson();
bool ParseJson(const string& _sJson);
bool GetString(const string& _sKey, string& _sValue);
bool GetInt(const string& _sKey, int& _iValue);
bool GetBool(const string& _sKey, bool& _bValue);
bool GetDouble(const string& _sKey, double& _dValue);
bool GetArray(const string& _sKey, Value& _aryValue);
bool GetObject(const string& _sKey, Value& _objValue);
const char* GetLastError();
private:
bool _GetValue(const string& _sKey, int _iType, Value& _Value);
private:
/**
* @brief The Type enum
* 是字段类型枚举
*/
enum Type {
Int = 0x0,///整型
Bool = 0x1,///<bool值
Double = 0x2,///<浮点型
String = 0x3,///<字符串
Array = 0x4,///<json数组
Object = 0x5,///<json对象
Undefined = 0x80///<无定义
};
Value m_jsonObj; ///<解析后的json对象
string m_sError; ///<解析过程出现的错误信息
bool m_bSucceess; ///<解析是否成功标志位
};
#include "OprJson.h"
#include <sstream>
CParseJson::CParseJson()
:m_bSucceess(false)
{
}
bool CParseJson::ParseJson(const string& _sJson)
{
Reader reader;
if (false == (m_bSucceess = reader.parse(_sJson, m_jsonObj)))
{
m_sError = reader.getFormatedErrorMessages();
}
return m_bSucceess;
}
bool CParseJson::GetString(const string& _sKey, string& _sValue)
{
Value value;
if (_GetValue(_sKey, String, value))
{
_sValue = value.asString();
return true;
}
return false;
}
bool CParseJson::GetInt(const string& _sKey, int& _iValue)
{
Value value;
if (_GetValue(_sKey, Int, value))
{
_iValue = value.asInt();
return true;
}
return false;
}
bool CParseJson::GetBool(const string& _sKey, bool& _bValue)
{
Value value;
if (_GetValue(_sKey, Bool, value))
{
_bValue = value.asBool();
return true;
}
return false;
}
bool CParseJson::GetDouble(const string& _sKey, double& _dValue)
{
Value value;
if (_GetValue(_sKey, Double, value))
{
_dValue = value.asDouble();
return true;
}
return false;
}
bool CParseJson::GetArray(const string& _sKey, Value& _aryValue)
{
return _GetValue(_sKey, Array, _aryValue);
}
bool CParseJson::GetObject(const string& _sKey, Value& _objValue)
{
return _GetValue(_sKey, Object, _objValue);
}
const char* CParseJson::GetLastError()
{
return m_sError.c_str();
}
bool CParseJson::_GetValue(const string& _sKey, int _iType, Value& _Value)
{
if (false == m_bSucceess)
{
return false;
}
if (false == m_jsonObj.isMember(_sKey))
{
ostringstream stream;
stream << "json object not found key:" << _sKey;
m_sError = stream.str();
return false;
}
_Value = m_jsonObj[_sKey];
string sErrorType;
bool bType = false; //类型是否正确
switch (_iType)
{
case Int:
if (false == (bType = _Value.isInt()))
{
sErrorType = "Int";
}
break;
case String:
if (false == (bType = _Value.isString()))
{
sErrorType = "String";
}
break;
case Bool:
if (false == (bType = _Value.isBool()))
{
sErrorType = "Bool";
}
break;
case Double:
if (false == (bType = _Value.isDouble()))
{
sErrorType = "Double";
}
break;
case Array:
if (false == (bType = _Value.isArray()))
{
sErrorType = "Array";
}
break;
case Object:
if (false == (bType = _Value.isObject()))
{
sErrorType = "Object";
}
break;
default:
bType = false;
break;
}
if (false == bType)
{
ostringstream stream;
stream << "the key:" << _sKey << " is not " << sErrorType << " type";
m_sError = stream.str();
}
return bType;
}
#pragma once
#include <json.h>
using namespace Json;
using namespace std;
class CParseJson
{
public:
CParseJson();
bool ParseJson(const string& _sJson);
bool GetString(const string& _sKey, string& _sValue);
bool GetInt(const string& _sKey, int& _iValue);
bool GetBool(const string& _sKey, bool& _bValue);
bool GetDouble(const string& _sKey, double& _dValue);
bool GetArray(const string& _sKey, Value& _aryValue);
bool GetObject(const string& _sKey, Value& _objValue);
const char* GetLastError();
private:
bool _GetValue(const string& _sKey, int _iType, Value& _Value);
private:
/**
* @brief The Type enum
* 是字段类型枚举
*/
enum Type {
Int = 0x0,///整型
Bool = 0x1,///<bool值
Double = 0x2,///<浮点型
String = 0x3,///<字符串
Array = 0x4,///<json数组
Object = 0x5,///<json对象
Undefined = 0x80///<无定义
};
Value m_jsonObj; ///<解析后的json对象
string m_sError; ///<解析过程出现的错误信息
bool m_bSucceess; ///<解析是否成功标志位
};
#include "OprJson.h"
#include <sstream>
CParseJson::CParseJson()
:m_bSucceess(false)
{
}
bool CParseJson::ParseJson(const string& _sJson)
{
Reader reader;
if (false == (m_bSucceess = reader.parse(_sJson, m_jsonObj)))
{
m_sError = reader.getFormatedErrorMessages();
}
return m_bSucceess;
}
bool CParseJson::GetString(const string& _sKey, string& _sValue)
{
Value value;
if (_GetValue(_sKey, String, value))
{
_sValue = value.asString();
return true;
}
return false;
}
bool CParseJson::GetInt(const string& _sKey, int& _iValue)
{
Value value;
if (_GetValue(_sKey, Int, value))
{
_iValue = value.asInt();
return true;
}
return false;
}
bool CParseJson::GetBool(const string& _sKey, bool& _bValue)
{
Value value;
if (_GetValue(_sKey, Bool, value))
{
_bValue = value.asBool();
return true;
}
return false;
}
bool CParseJson::GetDouble(const string& _sKey, double& _dValue)
{
Value value;
if (_GetValue(_sKey, Double, value))
{
_dValue = value.asDouble();
return true;
}
return false;
}
bool CParseJson::GetArray(const string& _sKey, Value& _aryValue)
{
return _GetValue(_sKey, Array, _aryValue);
}
bool CParseJson::GetObject(const string& _sKey, Value& _objValue)
{
return _GetValue(_sKey, Object, _objValue);
}
const char* CParseJson::GetLastError()
{
return m_sError.c_str();
}
bool CParseJson::_GetValue(const string& _sKey, int _iType, Value& _Value)
{
if (false == m_bSucceess)
{
return false;
}
if (false == m_jsonObj.isMember(_sKey))
{
ostringstream stream;
stream << "json object not found key:" << _sKey;
m_sError = stream.str();
return false;
}
_Value = m_jsonObj[_sKey];
string sErrorType;
bool bType = false; //类型是否正确
switch (_iType)
{
case Int:
if (false == (bType = _Value.isInt()))
{
sErrorType = "Int";
}
break;
case String:
if (false == (bType = _Value.isString()))
{
sErrorType = "String";
}
break;
case Bool:
if (false == (bType = _Value.isBool()))
{
sErrorType = "Bool";
}
break;
case Double:
if (false == (bType = _Value.isDouble()))
{
sErrorType = "Double";
}
break;
case Array:
if (false == (bType = _Value.isArray()))
{
sErrorType = "Array";
}
break;
case Object:
if (false == (bType = _Value.isObject()))
{
sErrorType = "Object";
}
break;
default:
bType = false;
break;
}
if (false == bType)
{
ostringstream stream;
stream << "the key:" << _sKey << " is not " << sErrorType << " type";
m_sError = stream.str();
}
return bType;
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- XML 与 JSON 优劣对比
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- VBA将excel数据表生成JSON文件
- newtonsoft.json解析天气数据出错解决方法
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- vbs 解析json jsonp的方法
- C++联合体转换成C#结构的实现方法
- Extjs4如何处理后台json数据中日期和时间
- C#实现将类的内容写成JSON格式字符串的方法
- JQuery ajax返回JSON时的处理方式 (三种方式)
- jquery JSON的解析方式示例介绍
- c#版json数据解析示例分享
- ASP JSON类文件的使用方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏