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

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

首先在工程中添加json类库


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

里面的内容为:

[

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

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

]

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


#ifndef __Cocos2dxApp__Student__

#define __Cocos2dxApp__Student__


#include <stdio.h>

#include "cocos2d.h"


class Student :publiccocos2d::Node

{

public:

CREATE_FUNC(Student);

CC_SYNTHESIZE(int,m_nID,ID);

CC_SYNTHESIZE(int,m_nAge,Age);

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

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

};

#endif /* defined(__Cocos2dxApp__Student__) */


Student.cpp的内容如下:


#include "Student.h"

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

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

m_vcStudents中

class HelloWorld : public cocos2d::LayerColor

{

public:

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

static cocos2d::Scene* createScene();

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

virtual bool init();

// a selector callback

void menuCloseCallback(cocos2d::Ref* pSender);

bool loadStudentCfg(conststd::string& path);

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

CREATE_FUNC(HelloWorld);

private:

cocos2d::Vector<Student*> m_vcStudents;

};


loadStudentCfg函数代码如下:


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

{

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

Json::Reader reader;

Json::Value root;

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

return false;

}

// 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 (auto r : root) {

auto student = Student::create();

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

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

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

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

student->setID(id);

student->setAge(age);

student->setAddress(address);

student->setPhone(phone);

m_vcStudents.pushBack(student);

}

return@H_319_403@true;

}

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

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

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

如下:


bool HelloWorld::init()

{

// 1. super init first

if ( !LayerColor::init())

{

return false;

}

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

return false;

}

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

for (auto s : m_vcStudents)

{

int id = s->getID();

int age = s->getAge();

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

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

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

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

auto size = lbel->getContentSize();

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

height += size.height;

this->addChild(lbel);

}

return true;

}



最后打印结果如下:



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


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

#include "json/document.h"


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

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

rapidjson::Document reader;

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

int size = root.size();

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

auto student = Student::create();

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

int age = reader[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);

}




}

推荐文章

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

原文链接:https://www.f2er.com/cocos2dx/345367.html

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