#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 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::D ocument Document; Document. SetObject (); Rapidjson::D ocument::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", 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;}
cocos2d-x3.1 Generating JSON strings