解析json文件

来源:互联网 发布:办公室平台软件 编辑:程序博客网 时间:2024/05/18 03:11

头文件:

#include "json/document.h"#include "json/stringbuffer.h"#include "json/writer.h"

代码:

复制代码
bool UserManage::LoadUsers(){    bool bret=false;    do     {        string jsStr=FileUtils::getInstance()->getStringFromFile(_userCfgPath);        rapidjson::Document doc;        doc.Parse<0>(jsStr.c_str());        if (doc.HasParseError())        {            CCLOG("UserManage::LoadUsers parse json error!");            break;        }        if (doc.HasMember("UserList"))        {            const rapidjson::Value& userListValue=doc["UserList"];            if (userListValue.IsArray()&&userListValue.Size()>0)            {                _userMap.clear();                int userCount=userListValue.Size();                for (unsigned int i=0;i<userCount;i++)                {                    const rapidjson::Value &userValue=userListValue[i];                    if (userValue.IsObject())                    {                        User *pUser=new User();                        pUser->_userId=userValue["UserId"].GetInt();                        pUser->_accountName=userValue["AccountName"].GetString();                        pUser->_password=userValue["Password"].GetString();                        pUser->_registerTime=userValue["RegisterTime"].GetInt64();                        pUser->_vipMoney=userValue["VipMoney"].GetInt();                        pUser->_vipLevel=userValue["VipLevel"].GetInt();                        pUser->_roleId=userValue["RoleId"].GetInt();                        _userMap[pUser->_userId]=pUser;                    }                }            }        }        bret=true;    } while (0);    return bret;}
复制代码
0 0
原创粉丝点击