[COCOS2DX 3.0 (d)] Cocostudio generate JSON file + get JSON data using Rapidjson __JS

Source: Internet
Author: User

Use JSON in the game to store data, easy to read, and easy to manage.

Cocostudio can help generate JSON files in order to import data from Excel.

Cocostudio Download Address: http://www.cocostudio.org/


I. Using Cocostudio to generate JSON files

1. Edit the data in Excel first

The last "penetrating" property, should have been bool type, but because the Cocostudio Chinese version will directly change "true", "false" to "true", "false" is not very convenient to read, and because JSON in the recognition of BOOL is directly recognized "true", " False "string, which requires more characters, so I prefer to use 0 directly instead, so that when stored in a file with only one character, you can reduce the size of the file.


2. Open Cocostudio, select Data Editor, and then import the saved Excel.


You can edit the property data after opening (Key->value)


3. Click Export JSON file


Here you can choose to export two formats:

1. Property mode

[{"ID": 1, "name": "Tree", "Blood volume": 500, "defensive force": 100, "Attack": 0, "can penetrate": 0}
, {"ID": 2, "name": "Shrub", "blood volume": 150, "defensive force": 50, "Attack": 2, "can penetrate": 1 }
, {"ID": 3, "name": "Grass", "Blood volume": 50, "defensive force": 20, "attack": 0, "can penetrate": 1}
, {"ID": 4, "name": "Dog", "Blood volume": 200, "defensive force": 40, "attack": 20, " can penetrate ": 0}]

This format also writes property names into objects, which are suitable for object properties and generate larger files.

2. Object mode

[["ID", "name", "Blood volume", "Defensive force", "attack", "penetrating"]
, [1, "Tree", 500,100,0,0]
, [2, "shrub", 150,50,2,1]
, [3, "Bushes", 50,20,0,1]
, [4, "Dog", 200,40,20,0]
]

This type aligns the attributes and values, the file is smaller but requires the same format

two. Get JSON data using Rapidjson

COCOS2DX 3.0 comes with Rapidjson support files that don't need to be added like Jsoncpp, and Rapidjson because he not only supports all platforms, but also accesses efficiency just as much as his name does to Jsoncpp. It is also very convenient to use. All aspects are perfect, of course, to use it.

When used, just add the header file

#include "json/rapidjson.h"
#include "json/document.h"
//#include "json/filestream.h"
//#include JSON /prettywriter.h "
//#include" json/writer.h "
//#include" json/stringbuffer.h "
If you just read the JSON, just add the first two files.

Use is also very convenient, use the above two formats for example, write read the data code.

1. Json reads stored on the property way

BOOL ShowJsonData1 () {string filename= "Json_test_1.json";
	Rapidjson::D ocument doc; Determine if the file exists if (!
		Fileutils::getinstance ()->isfileexist (filename) {log ("JSON file isn't find [%s]", filename);
	return false;
	//Read file data, initialize Doc std::string data=fileutils::getinstance ()->getstringfromfile (filename); Doc.
	Parse<rapidjson::kparsedefaultflags> (Data.c_str ()); Determines whether the read is successful or not and is an array type if (Doc. Hasparseerror () | | !doc.
		IsArray ()) {log ("Get JSON data err!");
	return false; for (unsigned int i=0;i<doc.
		
		Size (); i++) {//extract array elements individually (declared variables must be references) Rapidjson::value &v=doc[i]; int id;//id string name;//name int hp;//Blood int defense;//defensive force int attack;//attack int passable;//can penetrate (pass)//judge the genus Whether sex exists (can support Chinese (UTF8 format)) if (V.hasmember ("ID") && V.hasmember (a2u ("name")) && V.hasmember (A2u ("Blood volume") & & V.hasmember ("Defensive force") && V.hasmember (a2u ("attack")) && V.hasmember (a2u ("penetrating")) {//Extract data by attribute I d=v["ID"].
			GetInt (); Name=v[A2u ("name")].
			GetString (); HP=V[A2U ("Blood volume")].
			GetInt (); DEFENSE=V[A2U ("defensive Force")].
			GetInt (); ATTACK=V[A2U ("Attack")].
			GetInt (); PASSABLE=V[A2U ("can penetrate")].

			GetInt (); Log (a2u ("id:%d, Name:%s, blood Volume:%d, defensive force:%d, attack:%d, penetrability:%s"), Id,name.c_str (), Hp,defense,attack, (passable?
		"True": "false");
} return true;
 }
As long as the Chinese into the UTF8 format is unfortunately through the Chinese subscript to get the data, A2U macro Please see my last article, is to convert BGK to UTF8 can be cocos2dx on the use of Chinese.

2. Json reads stored on object mode

BOOL ShowJsonData2 () {string filename= "Json_test_1.json";
	Rapidjson::D ocument doc; Determine if the file exists if (!
		Fileutils::getinstance ()->isfileexist (filename) {log ("JSON file isn't find [%s]", filename);
	return false;
	//Read file data, initialize Doc std::string data=fileutils::getinstance ()->getstringfromfile (filename); Doc.
	Parse<rapidjson::kparsedefaultflags> (Data.c_str ()); Determines whether the read is successful or not and is an array type if (Doc. Hasparseerror () | | !doc.
		IsArray ()) {log ("Get JSON data err!");
	return false; //starts at line 2nd because the first row is the property for (unsigned int i=1;i<doc).

		Size (); i++) {//extract array elements individually (declared variables must be references) Rapidjson::value &v=doc[i];
		int id;//id string name;//name int hp;//Blood int defense;//defensive force int attack;//attack int passable;//can penetrate (pass)//by subscript extraction
		int a=0; id=v[a++].
		GetInt (); name=v[a++].
		GetString (); hp=v[a++].
		GetInt (); defense=v[a++].
		GetInt (); attack=v[a++].
		GetInt (); passable=v[a++].

		GetInt (); Log (a2u ("id:%d, Name:%s, blood Volume:%d, defensive force:%d, attack:%d, penetrability:%s"), Id,name.c_str (), Hp,defense,attack,(passable?)
	"True": "false");
return true; }

*****************************************************************************

Check out the output:



About the JSON operation to learn this, and finally strongly recommend everyone to use Rapidjson, and rapidjson than jsoncpp weak burst.

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.