Json file generation and reading in cocos2dx 3.X, cocos2dxjson

Source: Internet
Author: User

Json file generation and reading in cocos2dx 3.X, cocos2dxjson

Cocos2d-x 3.0 adds the rapidjson library for json parsing. Located in cocos2d/external/json of the project.

Rapidjson is a visible code base that can be run without the need to include. lib and. dll. For the project wiki, see here. The following two instances are used to gain an in-depth understanding of its usage in cocos2dx.

Generate and save the JSON File
# Include "CCStdC. h "# include" cocos2d. h "# include" json/document. h "# include" json/writer. h "# include" json/stringbuffer. h "using namespace rapidjson; USING_NS_CC; int main () {// *** generate a json file, which is stored in the *** rapidjson: Document writedoc; writedoc in the getWritablePath folder. setObject (); rapidjson: Document: AllocatorType & allocator = writedoc. getAllocator (); rapidjson: Value array (rapidjson: kArrayType); rapidjson: Value object (rapidjson: kObjectType ); // Add "name/value" to object in json object format. addMember ("inttag", 1, allocator); object. addMember ("doubletag", 1.0, allocator); object. addMember ("booltag", true, allocator); object. addMember ("hellotag", "helloworld", allocator); // Add array to json. pushBack (object, allocator); // Add "name/value" to writedoc in json object format. addMember ("json", "json string", allocator); writedoc. addMember ("array", array, allocator); StringBuffer buffer; rapidjson: Writer <StringBuffer> writer (buffer); writedoc. accept (writer); auto path = FileUtils: getInstance ()-> getWritablePath (); path. append ("myhero. json "); FILE * file = fopen (path. c_str (), "wb"); if (file) {fputs (buffer. getString (), file); fclose (file) ;}cclog ("% s", buffer. getString (); return 0 ;}


I compiled it with VS2012, and the final generated json file is located in the \ proj. win32 \ Debug. win32 folder. The content is as follows:

{"Json": "json string", "array": [{"inttag": 1, "doubletag": 1, "booltag": true, "hellotag ": "helloworld"}]}

Read and display the JSON File

The Parsing Method of rapidjson needs to be compiled separately according to the original json format. Therefore, based on the above generation method, the parsing method should be:

# Include "CCStdC. h "# include" cocos2d. h "# include" json/document. h "# include" json/writer. h "# include" json/stringbuffer. h "using namespace rapidjson; USING_NS_CC; int main () {auto path = FileUtils: getInstance ()-> getWritablePath (); path. append ("myhero. json "); // *** read the json file *** rapidjson: Document readdoc; bool bRet = false; ssize_t size = 0; std: string load_str; // If getFileData is not specified, the read root directory is the Resource folder unsign. Ed char * titlech = FileUtils: getInstance ()-> getFileData (path, "r", & size); load_str = std: string (const char *) titlech, size); // load_str = cocos2d: FileUtils: getInstance ()-> getStringFromFile (".. \ myhero. json "); readdoc. parse <0> (load_str.c_str (); if (readdoc. hasParseError () {CCLOG ("GetParseError % s \ n", readdoc. getParseError ();} if (! Readdoc. isObject () return 0; rapidjson: Value & _ json = readdoc ["json"]; const char * ch = _ json. getString (); cocos2d: log (ch); cocos2d: log (_ json. getString (); rapidjson: Value & _ array = readdoc ["array"]; if (_ array. isArray () {CCLOG ("test"); for (int I = 0; I <_ array. capacity (); I ++) {// CCLOG ("% d", I); rapidjson: Value & arraydoc = _ array [I]; if (arraydoc. hasMember ("inttag") {int _ inttag = arraydoc ["inttag"]. getInt (); CCLOG ("% d", _ inttag) ;}if (arraydoc. hasMember ("doubletag") {double _ doubletag = arraydoc ["doubletag"]. getDouble (); CCLOG ("% lf", _ doubletag);} if (arraydoc. hasMember ("booltag") {bool _ booltag = arraydoc ["booltag"]. getBool (); CCLOG ("% d", _ booltag);} if (arraydoc. hasMember ("hellotag") {const char * _ hellotag = arraydoc ["hellotag"]. getString (); CCLOG ("% s", _ hellotag) ;}} return 0 ;}
The CCLOG is displayed as follows:

Json string
Json string
Test
1
1.000000
1
Helloworld

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.