cocos2dx3.4 解析json文件

头文件:

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

}

 

你可能感兴趣的:(cocos2dx)