Cocos2d-x3.1 to generate Json strings

Source: Internet
Author: User

Cocos2d-x3.1 to generate Json strings

#include "HelloWorldScene.h"#include "json/document.h"#include "json/writer.h"#include "json/stringbuffer.h"#include 
 
  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 instancebool 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
  
    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;}
  
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.