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

Cocos2d-x3.1生成Json串

时间:2014-09-09 16:16:08      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:cocos2d-x3.1   json   

#include "HelloWorldScene.h"
#include "json/document.h"
#include "json/writer.h"
#include "json/stringbuffer.h"
#include <iostream>
using namespace rapidjson;
USING_NS_CC;

Scene* HelloWorld::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();
    
    // 'layer' is an autorelease object
    auto layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    bool bRet = false;
    do{
        CC_BREAK_IF(!Layer::init());

        auto path = FileUtils::getInstance()->getWritablePath();
        log("path = %s",path.c_str());
        path.append("test.json");
        
        rapidjson::Document document;
        document.SetObject();

        rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
        rapidjson::Value array(rapidjson::kArrayType);
        rapidjson::Value array1(rapidjson::kArrayType);
        rapidjson::Value object(rapidjson::kObjectType);
        rapidjson::Value object1(rapidjson::kObjectType);

        object.AddMember("TransType","CALLCENTER", allocator);
        object.AddMember("TransCode",00011002, allocator);
        object.AddMember("SubTransCode",2, allocator);
        object.AddMember("TransDate",2013-6-17, allocator);
        object.AddMember("TransTime","15:23:25", allocator);
        object.AddMember("TransSeq",00011002-2201361700017, allocator);
        object.AddMember("Operator","web", allocator);
        object.AddMember("RowNumStart",1, allocator);
        object.AddMember("PageRowNum",1000, allocator);
        object.AddMember("PageFlag",0, allocator);
        object.AddMember("TotalRowNum","", allocator);
        object.AddMember("ResultCode","", allocator);
        object.AddMember("ResultMsg","", allocator);
        object.AddMember("OrderFlag",0, allocator);
        object.AddMember("OrderField","", allocator);
        array.PushBack(object, allocator);
        
        object1.AddMember("ContNo",662206117012, allocator);
        array1.PushBack(object1, allocator);
        
        document.AddMember("BaseInfo",array, allocator);
        document.AddMember("InputData",array1, allocator);
        
        
        StringBuffer buffer;
        rapidjson::Writer<StringBuffer> writer(buffer);
        document.Accept(writer);
        log("%s",buffer.GetString());
        
        FILE* fp = std::fopen(path.c_str(), "at+");
        CCASSERT(fp != NULL, "file open error");
        auto str = StringUtils::format("%s",buffer.GetString());
        auto length = str.length();
        log("length = %lu",str.length());
        fwrite(str.c_str(), length, 1, fp);
        fclose(fp);

        
        bRet = true;
    }while(0);
    return bRet;
}
bubuko.com,布布扣

Cocos2d-x3.1生成Json串

标签:cocos2d-x3.1   json   

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

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