zoukankan      html  css  js  c++  java
  • cocos2dx3.4 保存json文件

    头文件:

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

    代码:

    void UserManage::SaveUserToFile()
    {
        do 
        {
            CC_BREAK_IF(_userMap.size()==0);
    
            rapidjson::Document doc;
            doc.SetObject();
            rapidjson::Document::AllocatorType& allocator=doc.GetAllocator();
            rapidjson::Value uList(rapidjson::kArrayType);
    
            UserMap::iterator it;
            for(it=_userMap.begin();it!=_userMap.end();it++)
            {
                User *pUser=it->second;
                if (pUser)
                {
                    rapidjson::Value uValue(rapidjson::kObjectType);
                    uValue.AddMember("UserId",pUser->_userId,allocator);
                    uValue.AddMember("AccountName",pUser->_accountName.c_str(),allocator);
                    uValue.AddMember("Password",pUser->_password.c_str(),allocator);
                    uValue.AddMember("RegisterTime",pUser->_registerTime,allocator);
                    uValue.AddMember("VipMoney",pUser->_vipMoney,allocator);
                    uValue.AddMember("VipLevel",pUser->_vipLevel,allocator);
                    uValue.AddMember("RoleId",pUser->_roleId,allocator);
    
                    uList.PushBack(uValue,allocator);
                }
            }
            rapidjson::StringBuffer buff;
            rapidjson::Writer<rapidjson::StringBuffer> writer(buff);
            doc.Accept(writer);
    
            FILE* pFile=fopen(_userCfgPath.c_str(),"wb");
            if (pFile)
            {
                fputs(buff.GetString(),pFile);
                fclose(pFile);
            }else
            {
                CCLOG("UserManage::SaveUserToFile error! can not open %s",_userCfgPath.c_str());
            }
    
        } while (0);
    
    }
  • 相关阅读:
    CodeForces
    EOJ 3506. 斐波那契数列
    牛客练习赛13 D幸运数字Ⅳ . 康托逆展开
    UVA
    Piggy-Bank HDU
    Dollar Dayz POJ
    UVA 674 Coin Change (完全背包)
    python OOP (1)
    python lambda简易使用
    python whl模块安装方法
  • 原文地址:https://www.cnblogs.com/gamesky/p/4281453.html
Copyright © 2011-2022 走看看