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<std::string>& strName,const std::vector<std::string>& 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<std::string>& strName,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<StringBuffer> 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<std::string> strName = getStrName();
        std::vector<std::string> 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<std::string> HelloWorld::getStrName()
{
    std::vector<std::string> 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<std::string> HelloWorld::getStrContent()
{
    std::vector<std::string> 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、结果

相关文章

操作步骤 1、创建cocos2d-x工程 2、新建 Scene1.cpp Scene1.h Scene1.h代码 #ifndef __SCENE1_H__#defi...
开发环境:OS(WINDOWS 8.1 X64 企业版) cocos2d-x 2.2.1 vs2010 想给vs安装上cocos的模版,执行Install...
把创建项目做成一个批处理,当创建项目时可以省时省力很多。 操作步骤 1、在 E:cocos2d-x-2.2.1toolspr...
https://www.cnblogs.com/JiaoQing/p/3906780.html 四个响应函数 1 EventListenerPhysicsContact* evC...
转载于 http://www.cnblogs.com/kenkofox/p/3926797.html 熟悉js的dom事件或者flash事件的,基本都能立...
ScrollView(滚动容器)加载大量item时会导致游戏界面的卡顿,严重时整个界面会出现卡死的情况。最近项...