码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2014-10-13 01:55:09      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:json   cocos2d-x   

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

bubuko.com,布布扣

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

标签:json   cocos2d-x   

原文地址:http://blog.csdn.net/yuxikuo_1/article/details/40031391

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!