Cocos2d-x的產生Json檔案的方法(續上續,哈哈)

來源:互聯網
上載者:User

Cocos2d-x的產生Json檔案的方法(續上續,哈哈)
1、Json產生工具升級版

本次Json產生工具升級版是經過封裝,只需傳入2個vector和1個int值即可完成Json檔案的產生。
2、具體代碼
1).h檔案
#include "cocos2d.h"#include "../cocos2d/external/json/document.h"#include "../cocos2d/external/json/writer.h"#include "../cocos2d/external/json/stringbuffer.h"USING_NS_CC;using namespace rapidjson;class JsonMake : public Ref{public:    CREATE_FUNC(JsonMake);    virtual bool init();    std::string getJsonMyBaodanMain(const std::vector& strName,const std::vector& strContent,const int count);};
2).cpp檔案
#include "JsonMake.h"bool JsonMake::init(){    bool bRet = false;    do{        bRet = true;    }while(0);    return bRet;}std::string JsonMake::getJsonMyBaodanMain(const std::vector& strName,const std::vector& strContent,const int count){        rapidjson::Document document;    document.SetObject();    CCASSERT(strName.size() == strContent.size(), "strName.size() != strContent.size()");    log("strName.size = %lu,strContent.size = %lu",strName.size(),strContent.size());    rapidjson::Document::AllocatorType& allocator = document.GetAllocator();    rapidjson::Value objectROOT(rapidjson::kArrayType);    log("count = %d",count);    for(int i = 0; i < count; i++)    {        rapidjson::Value object(rapidjson::kObjectType);        for(int j = 0 ; j < strName.size(); j++)        {            object.AddMember(strName.at(j).c_str(), strContent.at(j).c_str(), allocator);        }        objectROOT.PushBack(object,allocator);    }        document.AddMember("info", objectROOT, allocator);    StringBuffer buffer;    rapidjson::Writer writer(buffer);    document.Accept(writer);    log("%s",buffer.GetString());    return StringUtils::format("%s",buffer.GetString());}
3、使用方法
只需在HelloWorldScene.cpp中的init函數中添加如下代碼:
        auto path = FileUtils::getInstance()->getWritablePath();        log("path = %s",path.c_str());        path.append("myBaodanMain.json");        FILE* fp = std::fopen(path.c_str(), "at+");        CCASSERT(fp != NULL, "file open error");                auto jsonMake = JsonMake::create();        std::vector strName = getStrName();        std::vector strContent = getStrContent();        auto str = jsonMake->getJsonMyBaodanMain(strName,strContent,10);        auto length = str.length();        fwrite(str.c_str(), length, 1 ,fp);        fclose(fp);

然後再添加以下兩個函數的定義,當然在.h中的聲明是必不可少的呀!

std::vector HelloWorld::getStrName(){    std::vector strName;    strName.push_back("baodanTitle");    strName.push_back("baodanStatue");    strName.push_back("insuranceName");    strName.push_back("takeEffectTime");    strName.push_back("timeLimit");    strName.push_back("saleUnit");    strName.push_back("institutionAddress");    strName.push_back("toubaoPerson");    strName.push_back("beibaoPerson");    strName.push_back("forehead");    strName.push_back("premium");    return strName;}std::vector HelloWorld::getStrContent(){    std::vector strContent;    strContent.push_back("保單號");    strContent.push_back("保單狀態");    strContent.push_back("險種名稱");    strContent.push_back("生效日期");    strContent.push_back("保險期限");    strContent.push_back("銷售單位");    strContent.push_back("機構地址");    strContent.push_back("投保人");    strContent.push_back("被保人");    strContent.push_back("保額");    strContent.push_back("保費");        return strContent;}
4、結果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.