JSON file generation and reading in COCOS2DX 3.X

Source: Internet
Author: User

Cocos2d-x 3.0 joins the Rapidjson library for JSON parsing. Located under the Cocos2d/external/json of the project.

Rapidjson is a visible code library that does not need to contain. lib and. dll to run. See here for the project wiki. The following two examples are used to gain an in-depth understanding of its use in COCOS2DX.

Generate a JSON file and save
#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 () {//*** generates a JSON file, stored under the Getwritablepath folder * * * Rapidjson::D ocument writedoc;writedoc. SetObject (); Rapidjson::D ocument::allocatortype& allocator = Writedoc. Getallocator (); rapidjson::value Array (rapidjson::karraytype); Rapidjson::value object (Rapidjson::kobjecttype);// The JSON object format adds "name/value" to object. AddMember ("Inttag", 1, allocator), object. AddMember ("Doubletag", 1.0, allocator), object. AddMember ("Booltag", true, allocator), object. AddMember ("Hellotag", "HelloWorld", allocator);//JSON joins an array of arrays. Pushback (object, allocator);//JSON Object format adds "name/value" pair writedoc. 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 ();p ath.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 resulting JSON file is located under the \proj.win32\debug.win32 folder. Open the content as follows:

{"JSON": "JSON string", "array": [{"Inttag": 1, "Doubletag": 1, "Booltag": True, "Hellotag": "HelloWorld"}]}

Reads the JSON file and displays

Rapidjson need to write the parsing method separately according to the original JSON format, so according to 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 ();p ath.append ("Myhero.json");//*** Read JSON file ***rapidjson::D ocument readdoc;bool bRet = false;ssize_t size = 0;std::string load_str;//getfiledata If not specified, read root The directory is the Resource folder unsigned 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 (inti=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 final display of the Cclog is:

JSON string
JSON string
Test
1
1.000000
1
HelloWorld

JSON file generation and reading in COCOS2DX 3.X

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.