加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Cocos2d-x的生成Json文件的方法(续上续,哈哈)

发布时间:2020-12-14 19:03:54 所属栏目:百科 来源:网络整理
导读: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

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、结果

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读