首页 > Web开发 > 详细

cocos2dx3.4 解析json文件

时间:2015-02-09 15:46:04      阅读:329      评论:0      收藏:0      [点我收藏+]

头文件:

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

 

cocos2dx3.4 解析json文件

原文:http://www.cnblogs.com/gamesky/p/4281450.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!