首页 > 代码库 > Cocos2d-x3.1生成Json串
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; }
Cocos2d-x3.1生成Json串
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。