cocos2d-x中使用json
来源:互联网 发布:彩虹六号 知乎 编辑:程序博客网 时间:2024/06/07 23:40
首先去下载JsonCpp这个库,放到项目的Class目录下,再在项目中添加进来。
然后、、、然后看图。。。
//JsonTestLayer.h#pragma once#include "json/json.h"#include "cocos2d.h"USING_NS_CC;class JsonTestLayer : public CCLayer{public:CREATE_FUNC(JsonTestLayer);bool init();protected:};
//JsonTestLayer.cpp#include "JsonTestLayer.h"#include <string>#include <iostream>using namespace std;#define jsonFilePath "file/json_test.txt"#define LABEL_FONT_SIZE 28#define LABEL_FONT_NAME "Arial"enum TAG{ Tag_Label_Direction,Tag_Label_Format,Tag_Label_Id,Tag_Label_Name,Tag_Label_Sex,Tag_Label_Love,Tag_Label_Chat};bool JsonTestLayer::init(){bool bRet = false;do {CC_BREAK_IF( !CCLayer::init() );CCSize winSize = CCDirector::sharedDirector()->getWinSize();/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Json test///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////read fileunsigned long dataSize;unsigned char * pData;pData = CCFileUtils::sharedFileUtils()->getFileData(jsonFilePath, "r", &dataSize);if( pData==NULL || dataSize==0 ){CCLog("JsonTestLayer --> getdata json_test file error!");break;}//解析Json::Reader reader;Json::Value root;if ( !reader.parse(string(pData, pData+dataSize), root) ){CCLog("ERROR: JsonTestLayer --> parse json_test file error!");break;}// 1. directionstring strDirection = root["direction"].asString();CCLabelTTF* labelDirection = CCLabelTTF::create(strDirection.c_str(), LABEL_FONT_NAME, LABEL_FONT_SIZE, CCSize(winSize.width-60, 0), kCCTextAlignmentLeft);addChild(labelDirection, 1, Tag_Label_Direction);labelDirection->setAnchorPoint(ccp(0.5,1));labelDirection->setPosition(ccp(winSize.width/2, winSize.height-20));// 2. formatstring strFormat = root["format"].asString();CCLabelTTF* LabelFormat = CCLabelTTF::create(strFormat.c_str(), LABEL_FONT_NAME, LABEL_FONT_SIZE, CCSize(winSize.width-60, 0), kCCTextAlignmentLeft);addChild(LabelFormat, 1, Tag_Label_Format);LabelFormat->setAnchorPoint(ccp(0.5,1));LabelFormat->setPosition(ccp(winSize.width/2, labelDirection->getPositionY()-labelDirection->getContentSize().height-50));// 3. strings arrayJson::Value oneObj;Json::Value strArr = root["strings"];if( strArr.size() != 5){CCLog("ERROR: JsonTestLayer --> json file size is [%d]", strArr.size());break;}//for(Json::Value::iterator it= strArr.begin(); it!=strArr.end(); ++it)// array 1 -> id (int)oneObj = strArr[(Json::Value::UInt)0];int id = oneObj["id"].asInt();// array 2 -> name (string)oneObj = strArr[1];string name = oneObj["name"].asString();// array 3 -> sex (bool)oneObj = strArr[2];bool sex = oneObj["sex"].asBool();// array 4 -> love (null)oneObj = strArr[3];int love1 = oneObj["love"].asInt();string love2 = oneObj["love"].asString();////////// array 5 -> chatoneObj = strArr[4];string chat = oneObj["chat"].asString();char buf[20] = "";//sprintf(buf, "%d", id);CCLabelTTF* labelId = CCLabelTTF::create(buf, LABEL_FONT_NAME, LABEL_FONT_SIZE);addChild(labelId, 1, Tag_Label_Id);labelId->setPosition(ccp(winSize.width/2, winSize.height/2 - 50));//CCLabelTTF* labelName = CCLabelTTF::create(name.c_str(), LABEL_FONT_NAME, LABEL_FONT_SIZE);addChild(labelName, 1, Tag_Label_Name);labelName->setPosition(ccp(winSize.width/2, winSize.height/2-100));//if(sex) strcpy(buf, "male");else strcpy(buf, "female");CCLabelTTF* labelSex = CCLabelTTF::create(buf, LABEL_FONT_NAME, LABEL_FONT_SIZE);addChild(labelSex, 1, Tag_Label_Sex);labelSex->setPosition(ccp(winSize.width/2, winSize.height/2-150));////CCLabelTTF* labelChat = CCLabelTTF::create(chat.c_str(), LABEL_FONT_NAME, LABEL_FONT_SIZE, CCSize(winSize.width-60, 0), kCCTextAlignmentLeft);addChild(labelChat, 1, Tag_Label_Chat);labelChat->setPosition(ccp(winSize.width/2, winSize.height/2-200-labelChat->getContentSize().height/2));bRet = true;} while (0);return bRet;}
//json解析的文件。。
exe
——————————此人很懒、、、
0 0
- cocos2d-x中使用json
- cocos2d-x中使用json
- cocos2d-x中使用json
- cocos2d-x 使用libjson解析json
- 【cocos2d-x 3.5】Lua Json使用
- cocos2d-x 中使用lua
- Cocos2d-x 中使用多线程
- Cocos2d-x 中使用多线程
- Cocos2d-X中使用Socket
- Cocos2d-X中使用Socket
- cocos2d-x中使用sqlite
- cocos2d-x中使用Http
- cocos2d-x Json解析
- cocos2d-x读取json
- Cocos2d-x Json读写
- [寒江孤叶丶的Cocos2d-x之旅_16]Cocos2d-x LUA脚本中解析Json的方法
- Cocos2d-x学习(五):cocos2d-x解析json(使用libjson库)
- Cocos2d-x学习(五):cocos2d-x解析json(使用libjson库)
- 用CCRenderTexture和BlendFunc制作游戏教学时使用的黑色覆盖层
- 到底做运维还是做开发
- CCOrbitCamera卡牌翻转效果
- CCTableView的使用和注意事项
- 算法荐书(程序员练功+大众科普)
- cocos2d-x中使用json
- CCScale9Sprite的使用
- 一步步教你如何在 Visual Studio 2013 上使用 Github
- 解决TableView / ScrollView上的Menu问题(1滑出View区域还可点击2导致点击menu后View不能滑动)
- poj1753解题报告(枚举、组合数)
- poj1328解题报告(贪心、线段交集)
- OTRS的安装和配置
- poj3295解题报告(构造、算术表达式运算)
- Building a battery powered WiFi IoT Sensor with ESP8266, MS-5611 (GY-63), nodemcu and MQTT