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); } returntrue; } 代码中注释的部分是常规写法,现在我用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" 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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- c# – .net对象资源管理器控件
- ruby-on-rails – CKEditor Carrierwave Cloudin
- c# – Microsoft Graph – 无法以静默方式获取令
- ruby-on-rails – Rails:如何将has_one转换为ha
- cocos2dx,elipse,Errors occurred during the bu
- postgresql – 可以在单独的查询中存在两个具有相
- oracle – 使用TYPE属性检索VARRAY元素类型
- c – Xcode的新手,不能使用cout,只有std :: cout
- 全局变量相互依赖和初始化顺序的解决办法
- 【cocos3.9+box2d+tileMap】制作马里奥游戏(一)