cocos2d-x中使用json

 

首先去下载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 file
		unsigned 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. direction
		string 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. format
		string 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 array
		Json::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 -> chat
		oneObj = 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

 

 

 

——————————此人很懒、、、

 

 

 

posted @ 2014-01-17 15:19  、拂晓  阅读(1022)  评论(0编辑  收藏  举报