cocos2dx3.2 json读取数据简单使用

前端之家收集整理的这篇文章主要介绍了cocos2dx3.2 json读取数据简单使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

首先在工程中添加json类库


接着编写配置文件比如我的:StudentCfg.plist

里面的内容为:

[

{"id":1000001,"age":33,"address":"广西桂林红星县@H_301_13@","phone":"13132719203"},

{"id":1000001,"age":34,"phone":"13132719204"},"age":35,"phone":"13132719205"}

]

然后创建Student类,Student.h内容如下:


#ifndef __Cocos2dxApp__Student__

#define __Cocos2dxApp__Student__


#include <stdio.h>@H_301_13@

#include @H_301_13@"cocos2d.h"


class@H_301_13@ Student :public@H_301_13@cocos2d@H_301_13@::Node@H_301_13@

{

public:@H_301_13@

@H_301_13@CREATE_FUNC(@H_301_13@Student@H_301_13@);@H_301_13@

CC_SYNTHESIZE@H_301_13@(int@H_301_13@,m_nID@H_301_13@,ID);

CC_SYNTHESIZE@H_301_13@(int@H_301_13@,m_nAge@H_301_13@,Age);

CC_SYNTHESIZE@H_301_13@(std@H_301_13@::string@H_301_13@,m_strAddress@H_301_13@,Address);

CC_SYNTHESIZE@H_301_13@(std@H_301_13@::string@H_301_13@,m_strPhone@H_301_13@,Phone);

};

#endif @H_301_13@/* defined(__Cocos2dxApp__Student__) */


Student.cpp的内容如下:


#include @H_301_13@"Student.h"

,呵呵,这实现文件没没什么内容,主要是因为Student.h中的函数声明没有,这个学过c++的基本都懂

接下来,在HelloWorld.h中添加m_vcStudents,用以保存Student对象,添加loadStudentCfg函数,用来加载并解释json配置文件到@H_301_13@@H_301_13@

m_vcStudents中@H_301_13@

class@H_301_13@ HelloWorld : public@H_301_13@ cocos2d@H_301_13@::LayerColor@H_301_13@

{

public:@H_301_13@

@H_301_13@// there's no 'id' in cpp,so we recommend returning the class instance pointer

static@H_301_13@ cocos2d@H_301_13@::Scene@H_301_13@* createScene();

@H_301_13@// Here's a difference. Method 'init' in cocos2d-x returns bool,instead of returning 'id' in cocos2d-iphone

virtual@H_301_13@ bool@H_301_13@ init();

@H_301_13@// a selector callback

void@H_301_13@ menuCloseCallback(cocos2d@H_301_13@::Ref@H_301_13@* pSender);

bool@H_301_13@ loadStudentCfg(const@H_301_13@std@H_301_13@::string@H_301_13@& path);

@H_301_13@// implement the "static create()" method manually

@H_301_13@CREATE_FUNC(@H_301_13@HelloWorld@H_301_13@);@H_301_13@

private:@H_301_13@

cocos2d@H_301_13@::Vector@H_301_13@<Student@H_301_13@*> m_vcStudents;

};


loadStudentCfg函数代码如下:@H_301_13@

@H_301_13@


bool@H_301_13@ HelloWorld::loadStudentCfg(const@H_301_13@ std::string& path)

{

auto@H_301_13@ strData = FileUtils@H_301_13@::getInstance@H_301_13@()->getStringFromFile@H_301_13@(path.c_str@H_301_13@());

Json@H_301_13@::Reader@H_301_13@ reader;

Json@H_301_13@::Value@H_301_13@ root;

if@H_301_13@ (!reader.parse@H_301_13@(strData,root)) {

return@H_301_13@ false@H_301_13@;

}

@H_301_13@// int size = root.size();

// for (int i = 0; i < size; ++i) {

// auto student = Student::create();

// int id = root[i]["id"].asInt();

// int age = root[i]["age"].asInt();

// std::string address = root[i]["address"].asString();

// std::string phone = root[i]["phone"].asString();

//

// student->setID(id);

// student->setAge(age);

// student->setAddress(address);

// student->setPhone(phone);

// m_vcStudents.pushBack(student);

// }

for@H_301_13@ (auto@H_301_13@ r : root) {

auto@H_301_13@ student = Student@H_301_13@::create@H_301_13@();

int@H_301_13@ id = r["id"@H_301_13@].asInt@H_301_13@();

int@H_301_13@ age = r["age"@H_301_13@].asInt@H_301_13@();

std@H_301_13@::string@H_301_13@ address = r["address"@H_301_13@].asString@H_301_13@();

std@H_301_13@::string@H_301_13@ phone = r["phone"@H_301_13@].asString@H_301_13@();

student->setID@H_301_13@(id);

student->setAge@H_301_13@(age);

student->setAddress@H_301_13@(address);

student->setPhone@H_301_13@(phone);

m_vcStudents@H_301_13@.pushBack@H_301_13@(student);

} @H_301_13@

@H_301_13@return@H_301_13@@H_429_403@true@H_301_13@;@H_301_13@

}

代码中注释的部分是常规写法,现在我用c++11的新特性,for(:)语句

接着在HelloWorld.cpp实现文件的init函数调用@H_301_13@

loadStudentCfg,并将配置文件中的数据打印出来@H_301_13@@H_301_13@

如下:@H_301_13@@H_301_13@


@H_301_13@@H_301_13@

@H_301_13@@H_301_13@

bool@H_301_13@ @H_301_13@HelloWorld::init()@H_301_13@

{

@H_301_13@ @H_301_13@// 1. super init first

if@H_301_13@ ( !LayerColor@H_301_13@::init@H_301_13@())

{

@H_301_13@return @H_301_13@ false;@H_301_13@

}

@H_301_13@if@H_301_13@ (!@H_301_13@loadStudentCfg@H_301_13@(@H_301_13@"StudentCfg.plist")) {@H_301_13@

@H_301_13@return @H_301_13@ false;@H_301_13@

}

@H_301_13@auto@H_301_13@ visibleSize =Director@H_301_13@::getInstance@H_301_13@()->getVisibleSize@H_301_13@();

for@H_301_13@ (auto@H_301_13@ s : m_vcStudents)

{

int@H_301_13@ id = s->getID@H_301_13@();

int@H_301_13@ age = s->getAge@H_301_13@();

std@H_301_13@::string@H_301_13@ address = s->getAddress@H_301_13@();

std@H_301_13@::string@H_301_13@ phone = s->getPhone@H_301_13@();

auto@H_301_13@ msg =String@H_301_13@::createWithFormat@H_301_13@("id = %d,age= %d,address= %s,phe = %s"@H_301_13@,id,age,address.c_str@H_301_13@(),phone.c_str@H_301_13@());

auto@H_301_13@ lbel =Label@H_301_13@::create@H_301_13@(msg->getCString@H_301_13@(),"Arial"@H_301_13@,24@H_301_13@);

auto@H_301_13@ size = lbel->getContentSize@H_301_13@();

lbel->setPosition@H_301_13@(Vec2@H_301_13@(visibleSize.width@H_301_13@ /2@H_301_13@,visibleSize.height@H_301_13@ - size.height@H_301_13@ /2@H_301_13@ - height));

height += size.height@H_301_13@;

this@H_301_13@->addChild@H_301_13@(lbel);

}

@H_301_13@return @H_301_13@ true;@H_301_13@

}



最后打印结果如下:



这只是针对新手,呵呵,我也是新手,仅当学习笔记,希望能帮到大家的忙。@H_301_13@


@H_301_13@@H_301_13@

补充:刚才看了下,cocos2dx3.2本身就带有json库,所有更加方便使用具体用法如下:
1、添加文件

#include @H_301_13@"json/document.h"


2、解释使用,看代码说话

@H_301_13@std@H_301_13@::@H_301_13@string@H_301_13@ sData =@H_301_13@FileUtils@H_301_13@::@H_301_13@getInstance()->@H_301_13@getStringFromFile(@H_301_13@"config.plist"@H_301_13@);@H_301_13@

@H_301_13@rapidjson::@H_301_13@Document reader;@H_301_13@

reader.Parse@H_301_13@<0@H_301_13@>(sData.c_str@H_301_13@());

int@H_301_13@ size = root.size();

for@H_301_13@ (int@H_301_13@ i =0@H_301_13@; i < size; ++i) {

auto@H_301_13@ student = Student::create();

int@H_301_13@ id = reader[i]["id"@H_301_13@].asInt();

int@H_301_13@ age = reader[i]["age"@H_301_13@].asInt();

std::string address = root[i]["address"@H_301_13@].asString();

std::string phone = root[i]["phone"@H_301_13@].asString();

student->setID(id);

student->setAge(age);

student->setAddress(address);

student->setPhone(phone);

m_vcStudents.pushBack(student);

}




}

推荐文章

http://cn.cocos2d-x.org/tutorial/show?id=1528

猜你在找的Cocos2d-x相关文章