
首先在工程中添加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_338_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;
}
最后打印结果如下:
这只是针对新手,呵呵,我也是新手,仅当学习笔记,希望能帮到大家的忙。
#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
总结以上是内存溢出为你收集整理的cocos2dx3.2 json读取数据简单使用全部内容,希望文章能够帮你解决cocos2dx3.2 json读取数据简单使用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)