cocos2dx3.2 json讀取數據簡單使用

首先在工程中添加json類庫


接着編寫配置文件比如我的:StudentCfg.plist

裏面的內容爲:

[

{"id":1000001, "age":33, "address":"廣西桂林紅星縣","phone":"13132719203"},

{"id":1000001, "age":34, "address":"廣西桂林紅星縣","phone":"13132719204"},

{"id":1000001, "age":35, "address":"廣西桂林紅星縣","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"


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    



    













發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章