Method for generating a Json file for a Cocos2d-x (continued)

Source: Internet
Author: User

Method for generating a Json file for a Cocos2d-x (continued)

 

1. JsonMake class
//.h#include cocos2d.h#include ../cocos2d/external/json/document.h#include ../cocos2d/external/json/writer.h#include ../cocos2d/external/json/stringbuffer.hUSING_NS_CC;using namespace rapidjson;class JsonMake : public Ref{public:    CREATE_FUNC(JsonMake);    virtual bool init();    std::string getJsonMyBaodanMain();};

//.cpp#include JsonMake.hbool JsonMake::init(){    bool bRet = false;    do{                                bRet = true;    }while(0);    return bRet;}std::string JsonMake::getJsonMyBaodanMain(){        rapidjson::Document document;    document.SetObject();        rapidjson::Document::AllocatorType& allocator = document.GetAllocator();    rapidjson::Value object1(rapidjson::kObjectType);    rapidjson::Value object2(rapidjson::kObjectType);    rapidjson::Value object3(rapidjson::kObjectType);    rapidjson::Value object4(rapidjson::kArrayType);        object1.AddMember(Name, Yuxikuo, allocator);    object1.AddMember(Age, 18, allocator);    object1.AddMember(Sex, man, allocator);        object2.AddMember(Name, WangDongGe, allocator);    object2.AddMember(Age, 32, allocator);    object2.AddMember(Sex, woman, allocator);    object3.AddMember(Name, zhanhong, allocator);    object3.AddMember(Age, 20, allocator);    object3.AddMember(Sex, man, allocator);    object4.PushBack(object1,allocator);    object4.PushBack(object2,allocator);    object4.PushBack(object3,allocator);        document.AddMember(info, object4, allocator);        StringBuffer buffer;    rapidjson::Writer
 
   writer(buffer);    document.Accept(writer);    log(%s,buffer.GetString());        return StringUtils::format(%s,buffer.GetString());}
 
2. Usage
Save the generated json file locally
        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();        auto str = jsonMake->getJsonMyBaodanMain();        auto length = str.length();        fwrite(str.c_str(), length, 1 ,fp);        fclose(fp);
3. Results
//json{info:[{Name:Yuxikuo,Age:18,Sex:man},{Name:WangDongGe,Age:32,Sex:woman},{Name:zhanhong,Age:20,Sex:man}]}


Related Article

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.