您的位置:首页 > 编程语言 > C语言/C++

c++ json封装---解析

2016-04-14 19:46 477 查看
首先要有编译的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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  json c++ 封装