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 std::vector<std::string>& 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<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、结果
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。