日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Cocos數(shù)據(jù)篇[3.4](2)——Json數(shù)據(jù)操作-創(chuàng)新互聯(lián)

【嘮叨】

創(chuàng)新互聯(lián)專注于企業(yè)全網(wǎng)營(yíng)銷推廣、網(wǎng)站重做改版、漯河網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5場(chǎng)景定制成都商城網(wǎng)站開發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為漯河等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。

    在游戲中使用Json來儲(chǔ)存數(shù)據(jù),既方便讀取,又方便管理。

    比如CocosStudio 1.6之前版本導(dǎo)出的資源擴(kuò)展名就是 .ExportJson 格式的。

    Cocos2d-x 3.x 加入了rapidjson庫(kù)用于json解析。位于external/json下。

    本節(jié)要介紹的就是:如何使用 rapidjson庫(kù) 來操作處理json文件。

【參考】

    http://www.w3school.com.cn/json/index.asp (W3School)

    http://cn.cocos2d-x.org/tutorial/show?id=1203 (【官方文檔】rapidjson用法)

    http://cn.cocos2d-x.org/tutorial/show?id=1556 (RapidJson解析)

    http://cn.cocos2d-x.org/tutorial/show?id=1528 (rapidjson獲取Json數(shù)據(jù)的實(shí)戰(zhàn)經(jīng)驗(yàn))


【Json簡(jiǎn)介】

    摘自:http://www.w3school.com.cn/json/index.asp

1、什么是Json?

    > JSON 指的是 JavaScript 對(duì)象表示法JavaScript Object Notation)。

    > JSON 是輕量級(jí)的存儲(chǔ)和文本數(shù)據(jù)交換格式,類似XML。

    > JSON 比 XML 更小、更快,更易解析。

    > JSON 具有自我描述性,更易理解。

    > JSON 獨(dú)立于語(yǔ)言 * 。

  *  JSON 使用 JavaScript 語(yǔ)法來描述數(shù)據(jù)對(duì)象,但是 JSON 仍然獨(dú)立于語(yǔ)言和平臺(tái)。

    *  JSON 解析器和 JSON 庫(kù)支持許多不同的編程語(yǔ)言。

2、語(yǔ)法規(guī)則

    JSON 語(yǔ)法是 JavaScript 對(duì)象表示法語(yǔ)法的子集。

    (1)數(shù)據(jù)在“名稱/值對(duì)”中,即 鍵值對(duì)(key-value)形式。

    (2)每條數(shù)據(jù)由“逗號(hào)”分隔。

    (3)“花括號(hào)”{ } 保存 對(duì)象。

    (4)“方括號(hào)”[ ] 保存 數(shù)組

 2.1、名稱/值對(duì)

    JSON 數(shù)據(jù)的書寫格式是:名稱/值對(duì)(鍵值對(duì) key-value)。

    名稱/值對(duì),包括字段名稱(在雙引號(hào)中),后面寫一個(gè)冒號(hào),然后是值。

//
	// "名稱" : "值"
	"firstName" : "John"
	
	// 錯(cuò)誤。名稱必須加雙引號(hào)""
	name : "Alice"
//

 2.2、值

    JSON的值可以是:

      > null

        > 邏輯值(boolean)

        > 數(shù)字(number)

        > 字符串(string,在雙引號(hào) " " 中)

        > 數(shù)組(在方括號(hào) [ ] 中)

        > 對(duì)象(在花括號(hào) { } 中)

  PS:即“名稱/值對(duì)”數(shù)據(jù)中,其名稱的冒號(hào)“ : ”后面對(duì)應(yīng)的值可以不是字符串,也可以是數(shù)字、數(shù)組、對(duì)象等。

 2.3、對(duì)象

    JSON 對(duì)象在花括號(hào)中書寫:{ } 。

  對(duì)象可以包含多個(gè)名稱/值對(duì)( 可以理解為對(duì)象的 屬性名/屬性值 )。

    PS:名稱必須要加雙引號(hào)" ",并且對(duì)象中只能包含名稱/值對(duì)的形式,不能只有一個(gè)值。

    如下所示:

//
	{ 
		"name":"John",       // 正確
		"age":23,            // 正確
		"array" : [1,2,3,4], // 正確。值可以為數(shù)組形式

		"hello world",       // 錯(cuò)誤。不能僅為一個(gè)值
		name : "John"        // 錯(cuò)誤。名稱必須加雙引號(hào)"name"
	}
//

 2.4、數(shù)組

    JSON 數(shù)組在方括號(hào)中書寫:[ ] 。

  數(shù)組可包含多個(gè)值(可以為null、邏輯值、數(shù)字、字符串、對(duì)象、數(shù)組)。

    PS:數(shù)組中只能包含值的形式,不能為名稱/值的形式。

    如下所示:

//
	[
		true,                       // 邏輯值Bool
		123,                        // 數(shù)字Number
		"888",                      // 字符串String
		"hello world",              // 字符串String
		{"name":"alice", "age":23}, // 對(duì)象Object
		[1,2,3,4],                  // 數(shù)組Object

		"name" : "John"             // 錯(cuò)誤。不能為 名稱/值 的形式
	]
//


【rapidjson】

    Cocos2d-x 3.x 加入了 rapidjson庫(kù),用于Json解析。位于external/json下。

    只支持標(biāo)準(zhǔn)的Json格式,一些非標(biāo)準(zhǔn)的Json格式不支持。一些常用的解析方法需要自己封裝。注意判斷解析節(jié)點(diǎn)是否存在。

    PS:解析的Json文件,根節(jié)點(diǎn)必須為對(duì)象、或數(shù)組。不然無法解析。

    如下所示:

Cocos數(shù)據(jù)篇[3.4](2) ——Json數(shù)據(jù)操作

1、添加頭文件

    如果只用于解析Json文件,只要前2行的頭文件即可。

//
	#include "json/rapidjson.h"
	#include "json/document.h"
	#include "json/writer.h"
	#include "json/stringbuffer.h"
	//#include "json/filestream.h"
	//#include "json/prettywriter.h"

	using namespace rapidjson; // 命名空間
//

2、Json數(shù)據(jù)解析

    Cocos封裝的 rapidjson庫(kù),只能解析對(duì)象格式、或數(shù)組格式的Json文件。

 2.1、解析對(duì)象格式的Json

  Json文件中的數(shù)據(jù),根節(jié)點(diǎn)為一個(gè)對(duì)象,所有屬性在大花括號(hào) { } 中。

    對(duì)象中的數(shù)據(jù),通過 名稱/值 的形式進(jìn)行訪問。

    Json文件內(nèi)容如下:

//
{
	"hello" : "world",
	"t"     : true,
	"f"     : false,
	"n"     : null,
	"i"     : 123,
	"pi"    : 3.1416,
	"array" : [1, 2, 3, 4],
	"object": {
		"name" : "alice",
		"age" : 23
	}
}
//

    Json解析使用舉例:

//
//[1] 讀取json文件內(nèi)容
	std::string str = FileUtils::getInstance()->getStringFromFile("testJson.json");
	CCLOG("%s", str.c_str());

//[2] 創(chuàng)建用于處理json代碼的類
	// 創(chuàng)建rapidjson::Document類:用于操作json代碼
	rapidjson::Document d;

//[3] 解析json文件內(nèi)容
	// 其中 rapidjson::kParseDefaultFlags = 0,默認(rèn)方式
	d.Parse(str.c_str());
	// d.Parse<0>(str.c_str());  // 也可以直接寫<0>

//[4] 判斷解析是否出錯(cuò)
	if (d.HasParseError()) {
		CCLOG("GetParseError %s\n",d.GetParseError());
		return;
	}

//[5] 獲取json中的數(shù)據(jù)
	// 判斷json文件是否為對(duì)象格式
	if (d.IsObject()) {

		// 是否有 "hello" 屬性
		if (d.HasMember("hello")) {
			CCLOG("%s", d["hello"].GetString()); // 方式一:直接獲取
		}
		// 是否有 "i" 屬性
		if (d.HasMember("i")) {
			rapidjson::Value& i = d["i"];        // 方式二:保存為rapidjson::Value&
			CCLOG("%d", i.GetInt());
		}

		// 數(shù)組
		if (d.HasMember("array")) {
			// 獲取數(shù)組中的元素:d["array"][i]
			for (int i = 0; i < d["array"].Size(); i++) {
				CCLOG("%d : %d", i, d["array"][i].GetInt());
			}

//            // 也可以這么寫
//            rapidjson::Value& array = d["array"];
//            for (int i = 0; i < array.Size(); i++) {
//                CCLOG("%d : %d", i, array[i].GetInt());
//            }
		}

		// 對(duì)象
		if (d.HasMember("object")) {
			// 判斷 "object" 屬性對(duì)應(yīng)的值,是否為一個(gè)對(duì)象
			if (d["object"].IsObject()) {
				// 轉(zhuǎn)化為 rapidjson::Value&
				rapidjson::Value& object = d["object"];
				CCLOG("%s", d["object"]["name"].GetString());
				CCLOG("%d", object["age"].GetInt());
			}
		}
	}
//

    控制臺(tái)輸出結(jié)果:

Cocos數(shù)據(jù)篇[3.4](2) ——Json數(shù)據(jù)操作

 2.2、解析數(shù)組格式的Json

  json文件中的數(shù)據(jù),根節(jié)點(diǎn)為一個(gè)數(shù)組,所有元素在一個(gè)大方括號(hào) [ ] 中。

    數(shù)組中的數(shù)據(jù),通過下標(biāo)的形式訪問元素值(下標(biāo)從0開始)。

    Json文件內(nèi)容如下:

//
	[
		true,
		123,
		"888",
		"hello world",
		{"name" : "alice", "age" : 23},
		[1,2,3,4]
	]
//

  Json解析使用舉例:

//
//[1] 讀取json文件內(nèi)容
	std::string str = FileUtils::getInstance()->getStringFromFile("testJson.json");
	CCLOG("%s", str.c_str());

//[2] 創(chuàng)建用于處理json代碼的類
	// 創(chuàng)建rapidjson::Document類:用于操作json代碼
	rapidjson::Document d;

//[3] 解析json文件內(nèi)容
	// 其中 rapidjson::kParseDefaultFlags = 0,默認(rèn)方式
	d.Parse(str.c_str());
	// d.Parse<0>(str.c_str());  // 也可以直接寫<0>

//[4] 判斷解析是否出錯(cuò)
	if (d.HasParseError()) {
		CCLOG("GetParseError %s\n",d.GetParseError());
		return;
	}

//[5] 獲取json中的數(shù)據(jù)
	// 判斷json文件是否為數(shù)組格式
	if (d.IsArray()) {
		
		rapidjson::Value& array = d;
		
		for (int i = 0; i < array.Size(); i++) {

			if (d[i].IsBool()) {   // 邏輯值
				CCLOG("%d is Bool : %d", i, array[i].GetBool());
			}
			if (d[i].IsNumber()) { // 數(shù)字
				CCLOG("%d is Number : %d", i, array[i].GetInt());
			}
			if (d[i].IsString()) { // 字符串
				CCLOG("%d is String : %s", i, array[i].GetString());
			}
			if (d[i].IsObject()) { // 對(duì)象
				rapidjson::Value& object = d[i];
				CCLOG("%d is Object : %s", i, array[i]["name"].GetString());
				CCLOG("%d is Object : %d", i, object["age"].GetInt());
			}
			if (d[i].IsArray()) {  // 數(shù)組
				for (int j = 0; j < array[i].Size(); j++) {
					CCLOG("[%d,%d] is Array : %d", i, j, array[i][j].GetInt());
				}
			}
		}
	}
//

    控制臺(tái)輸出結(jié)果:

Cocos數(shù)據(jù)篇[3.4](2) ——Json數(shù)據(jù)操作

3、Json數(shù)據(jù)存儲(chǔ)

 3.1、存儲(chǔ)為對(duì)象格式的Json

    使用舉例:

//
//[1] 創(chuàng)建用于處理json代碼的類
	// 創(chuàng)建rapidjson::Document類:用于操作json代碼
	rapidjson::Document d;

//[2] 獲取分配器
	rapidjson::Document::AllocatorType& allocator = d.GetAllocator();

//[3] 設(shè)置為對(duì)象格式 SetObject
	d.SetObject();

//[4] 添加數(shù)據(jù)
	//[4.1] 往json對(duì)象中添加數(shù)據(jù):名稱/值對(duì)
	rapidjson::Value object(rapidjson::kObjectType); // 創(chuàng)建對(duì)象

	object.AddMember("int", 1, allocator);         // 添加 "int" : 1
	object.AddMember("double", 1.1, allocator);    // 添加 "double" : 1.1
	object.AddMember("hello", "world", allocator); // 添加 "hello" : "world"

	//[4.2] 往json數(shù)組中添加數(shù)據(jù):值
	rapidjson::Value array(rapidjson::kArrayType); // 創(chuàng)建數(shù)組

	rapidjson::Value str(rapidjson::kStringType);  // 字符串
	rapidjson::Value obj(rapidjson::kObjectType);  // 對(duì)象
	str.SetString("hello"); // 設(shè)置str的值
	obj.AddMember("name", "alice", allocator);
	obj.AddMember("age", 23, allocator);

	array.PushBack(123, allocator);   // 添加數(shù)字
	array.PushBack("888", allocator); // 添加字符串,方式一
	array.PushBack(str, allocator);   // 添加字符串,方式二
	array.PushBack(obj, allocator);   // 添加對(duì)象

	//[4.3] 往對(duì)象格式的json文件中添加數(shù)據(jù)
	d.AddMember("hello", "world", allocator);
	d.AddMember("object", object, allocator);
	d.AddMember("array", array, allocator);

//[5] 將json數(shù)據(jù)寫入文件中
	StringBuffer buffer;
	rapidjson::Writer writer(buffer);
	d.Accept(writer);
	CCLOG("%s", buffer.GetString());

	FILE* file = fopen("/soft/cocos2d-x-3.4/projects/Demo34/Resources/testJson.json", "wb");
	if(file) {
		fputs(buffer.GetString(), file);
		fclose(file);
	}
//

    控制臺(tái)輸出結(jié)果:

Cocos數(shù)據(jù)篇[3.4](2) ——Json數(shù)據(jù)操作

    Json代碼整理一下,如下:

//
	{
		"hello" : "world",
		"object": { "int":1, "double":1.1, "hello":"world" },
		"array" : [ 123, "888", "hello", {"name":"alice", "age":23} ]
	}
//

 3.2、存儲(chǔ)為數(shù)組格式的Json

    使用方法與存儲(chǔ)為對(duì)象格式類似。

    使用舉例:

//
//[1] 創(chuàng)建用于處理json代碼的類
	// 創(chuàng)建rapidjson::Document類:用于操作json代碼
	rapidjson::Document d;

//[2] 獲取分配器
	rapidjson::Document::AllocatorType& allocator = d.GetAllocator();

//[3] 設(shè)置為數(shù)組格式 SetArray
	d.SetArray();

//[4] 添加數(shù)據(jù)
	rapidjson::Value object(rapidjson::kObjectType);
	object.AddMember("name", "alice", allocator);
	object.AddMember("age", 23, allocator);

	d.PushBack(123, allocator);
	d.PushBack("hello", allocator);
	d.PushBack(object, allocator);

//[5] 將json數(shù)據(jù)寫入文件中
	StringBuffer buffer;
	rapidjson::Writer writer(buffer);
	d.Accept(writer);
	CCLOG("%s", buffer.GetString());

	FILE* file = fopen("/soft/cocos2d-x-3.4/projects/Demo34/Resources/testJson.json", "wb");
	if(file) {
		fputs(buffer.GetString(), file);
		fclose(file);
	}
//

    控制臺(tái)輸出結(jié)果:

Cocos數(shù)據(jù)篇[3.4](2) ——Json數(shù)據(jù)操作

4、Json數(shù)據(jù)修改

    以對(duì)象格式的Json文件為例。

    Json文件內(nèi)容如下:

//
	{
		"hello" : "world",
		"array" : [1, 2, 3, 4],
		"object": {"name":"alice", "age":23 }
	}
//

    對(duì)Json文件數(shù)據(jù)進(jìn)行修改:

//
//[1] 讀取json文件內(nèi)容
	std::string str = FileUtils::getInstance()->getStringFromFile("/soft/cocos2d-x-3.4/projects/Demo34/Resources/testJson.json");

//[2] 創(chuàng)建用于處理json代碼的類、獲取分配器、解析json文件內(nèi)容
	rapidjson::Document d;
	rapidjson::Document::AllocatorType& allocator = d.GetAllocator();
	d.Parse<0>(str.c_str());

//[3] 判斷解析是否出錯(cuò)
	if (d.HasParseError()) {
		CCLOG("GetParseError %s\n",d.GetParseError());
		return;
	}

//[4] 修改Json文件的數(shù)據(jù)
	// 修改: "hello" 的值 "hello":"hehe"
	d["hello"].SetString("hehe");
	// 添加:對(duì)象的數(shù)據(jù) "newdata":"888"
	d.AddMember("newdata", "888", allocator);
	// 刪除:對(duì)象中的數(shù)據(jù) "object"
	d.RemoveMember("object");

//[5] 將json數(shù)據(jù)重新寫入文件中
	StringBuffer buffer;
	rapidjson::Writer writer(buffer);
	d.Accept(writer);
	CCLOG("%s", buffer.GetString());

	FILE* file = fopen("/soft/cocos2d-x-3.4/projects/Demo34/Resources/testJson.json", "wb");
	if(file) {
		fputs(buffer.GetString(), file);
		fclose(file);
	}
//

    控制臺(tái)輸出結(jié)果:

Cocos數(shù)據(jù)篇[3.4](2) ——Json數(shù)據(jù)操作


【常用操作】

    常用操作如下:

//
// 創(chuàng)建用于處理json文件的類
	rapidjson::Document d;
// 獲取分配器
	rapidjson::Document::AllocatorType& allocator = d.GetAllocator();
// 判斷是否解析錯(cuò)誤
	d.HasParseError();
	d.GetParseError();

// 解析json文件
	d.Parse<0>(const Ch *str);
// 將數(shù)據(jù)寫入json文件
	StringBuffer buffer;
	rapidjson::Writer writer(buffer);
	d.Accept(writer);

	FILE* file = fopen("/soft/cocos2d-x-3.4/projects/Demo34/Resources/testJson.json", "wb");
	if(file) {
		fputs(buffer.GetString(), file);
		fclose(file);
	}

// json數(shù)組操作
// json數(shù)組
	rapidjson::Value& array = d["array"];
	rapidjson::Value array(rapidjson::kArrayType);

	array.PushBack(T value, allocator);   // 向數(shù)組中添加值
	array.Size();  // 數(shù)組元素個(gè)數(shù)
	array.Clear(); // 清空數(shù)組元素
	array.Empty(); // 判斷數(shù)組元素是否為空

// json對(duì)象操作
	// json對(duì)象
	rapidjson::Value& object = d["object"];
	rapidjson::Value object(rapidjson::kObjectType);

	object.AddMember(const Ch *name, T value, allocator); // 向?qū)ο笾刑砑用Q/值對(duì)
	object.HasMember("key");    // 是否存在某名稱/值對(duì)
	object.RemoveMember("key"); // 刪除某名稱/值對(duì)


// 獲取值
	rapidjson::Value& value = d["key"]; // 對(duì)象格式,"key-value"
	rapidjson::Value& value = d[index]; // 數(shù)組格式,下標(biāo)idx為整數(shù)

	value.GetBool();   // 值為邏輯值
	value.GetInt();    // 值為整數(shù)
	value.GetUint();   // 值為無符號(hào)整數(shù)
	value.GetInt64();  // 值為64位整數(shù)
	value.GetUint64(); // 值為64位無符號(hào)整數(shù)
	value.GetDouble(); // 值為浮點(diǎn)數(shù)
	value.GetString(); // 值為字符串
	// 獲取值的類型,返回值為枚舉類型rapidjson::Type
	// 	enum Type {
	//		kNullType   = 0,  //!< null
	//		kFalseType  = 1,  //!< false
	//		kTrueType   = 2,  //!< true
	//		kObjectType = 3,  //!< object
	//		kArrayType  = 4,  //!< array 
	//		kStringType = 5,  //!< string
	//		kNumberType = 6,  //!< number
	//	};
	value.GetType();   

// 判斷值的類型
	rapidjson::Value& value = d["key"]; // 對(duì)象格式,"key-value"
	rapidjson::Value& value = d[index]; // 數(shù)組格式,下標(biāo)idx為整數(shù)

	value.IsNull(); // 是否為空,null
	value.IsBool()、IsTrue()、IsFalse();
	value.IsNumber()、IsInt()、IsUint()、IsUint64()、IsInt64()、IsDouble();
	value.IsArray();
	value.IsObject();
	value.IsString();

// 設(shè)置值
	rapidjson::Value& value = d["key"]; // 對(duì)象格式,"key-value"
	rapidjson::Value& value = d[index]; // 數(shù)組格式,下標(biāo)idx為整數(shù)
	rapidjson::Value value;

	value.SetNull();               // 設(shè)置為空值
	value.SetBool(bool b);         // 設(shè)置為邏輯值
	value.SetInt(int i);           // 設(shè)置為Int值
	value.SetUint(unsigned int u); // 設(shè)置為UInt值
	value.SetInt64(int64_t i64);   // 設(shè)置為Int64值
	value.SetUint64(uint64_t u64); // 設(shè)置為UInt64值
	value.SetDouble(double d);     // 設(shè)置為double值 
	value.SetArray();              // 設(shè)置為數(shù)組格式
	value.SetObject();             // 設(shè)置為對(duì)象格式
	value.SetString(const Ch *s);  // 設(shè)置為字符串值
//

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。


網(wǎng)頁(yè)名稱:Cocos數(shù)據(jù)篇[3.4](2)——Json數(shù)據(jù)操作-創(chuàng)新互聯(lián)
分享鏈接:http://www.dlmjj.cn/article/ccshhd.html