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

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

概述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"#inclu 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、结果
总结

以上是内存溢出为你收集整理的Cocos2d-x的生成Json文件的方法(续上续,哈哈)全部内容,希望文章能够帮你解决Cocos2d-x的生成Json文件的方法(续上续,哈哈)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1003253.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存