展会信息港展会大全

cocos2dx 3.X 中 json 文件生成与读取,cocos2dxjson 生成JSON文件并保存 读取JSON文件并显示
来源:互联网   发布日期:2015-09-28 15:14:26   浏览:1876次  

导读: cocos2dx 3.X 中 json 文件生成与读取,cocos2dxjson Cocos2d-x 3.0 加入了rapidjson库用于json解析。位于项目的cocos2d/extern...

cocos2dx 3.X 中 json 文件生成与读取,cocos2dxjson

Cocos2d-x 3.0 加入了rapidjson库用于json解析。位于项目的cocos2d/external/json下。

rapidjson 是一个不需要包含 .lib 和 .dll 即可运行的可见代码库。项目 wiki 见这里。下面通过两个实例来深入了解它在 cocos2dx 中的用法。

生成JSON文件并保存

#include "CCStdC.h"

#include "cocos2d.h"

#include "json/document.h"

#include "json/writer.h"

#include "json/stringbuffer.h"

using namespacerapidjson;

USING_NS_CC;

int main()

{

//*** 生成 json 文件,存储在 getWritablePath 文件夹下 ***

rapidjson::Document writedoc;

writedoc.SetObject();

rapidjson::Document::AllocatorType& allocator = writedoc.GetAllocator();

rapidjson::Value array(rapidjson::kArrayType);

rapidjson::Value object(rapidjson::kObjectType);

// json object 格式添加 “名称/值” 对

object.AddMember("inttag", 1, allocator);

object.AddMember("doubletag", 1.0, allocator);

object.AddMember("booltag", true, allocator);

object.AddMember("hellotag", "helloworld", allocator);

// json 加入数组

array.PushBack(object, allocator);

// json object 格式添加 “名称/值” 对

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();

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;

}

我是用 VS2012 编译的,最终生成的json文件位于 \proj.win32\Debug.win32 文件夹下。打开内容如下:

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

读取JSON文件并显示

rapidjson 需要根据原 json 格式单独编写解析方法,因此根据以上生成方法,解析方法应该为:

#include "CCStdC.h"

#include "cocos2d.h"

#include "json/document.h"

#include "json/writer.h"

#include "json/stringbuffer.h"

using namespacerapidjson;

USING_NS_CC;

int main()

{

auto path = FileUtils::getInstance()->getWritablePath();

path.append("myhero.json");

//*** 读取 json 文件 ***

rapidjson::Document readdoc;

bool bRet = false;

ssize_t size = 0;

std::string load_str;

// getFileData 如果不指定,读取根目录是 Resource 文件夹

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(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;

}

CCLOG 的最终显示为:

json string

json string

test

1

1.000000

1

helloworld

http://www.bkjia.com/Androidjc/919860.htmlwww.bkjia.comtruehttp://www.bkjia.com/Androidjc/919860.htmlTechArticlecocos2dx 3.X 中 json 文件生成与读取,cocos2dxjson Cocos2d-x 3.0 加入了rapidjson库用于json解析。位于项目的cocos2d/external/json下。 rapidjson 是一个不需要...

赞助本站

人工智能实验室

相关热词: android开发 应用开发

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港