Cocos2d-x3.1 Json 해석

4230 단어
#include "json/document.h" 
rapidjson::Document d1;   
 std::string jsonpath = FileUtils::getInstance()->fullPathForFilename(gate.c_str());
 std::string contentStr = FileUtils::getInstance()->getStringFromFile(jsonpath);
 d1.Parse<0>(contentStr.c_str());  int tag = 0;
std::string path = ""; 
const char *className = DICTOOL->getStringValue_json(d1, "classname");
    if(strcmp(className, "CCNode") == 0)
    {
	
		int count = DICTOOL->getArrayCount_json(d1, "gameobjects");
		 for (int i = 0; i < count; i++)
        {
            const rapidjson::Value &subDict = DICTOOL->getSubDictionary_json(d1, "gameobjects", i);
            if (!DICTOOL->checkObjectExist_json(subDict))
            {
                break;
            }
            
            const char *comName = DICTOOL->getStringValue_json(subDict, "classname");
			tag = DICTOOL->getIntValue_json(subDict, "objecttag");
			if(tag == Json_root  ){
                float x = DICTOOL->getFloatValue_json(subDict, "x");
                float y = DICTOOL->getFloatValue_json(subDict, "y");
                Point rooxy = Point(x,y);
                this->gateEdit(tag, subDict,  rooxy);
				int countj = DICTOOL->getArrayCount_json(subDict, "gameobjects");
				for (int j=0; j < countj; j++){
					const rapidjson::Value &subDictnode = DICTOOL->getSubDictionary_json(subDict, "gameobjects", j);
					if (!DICTOOL->checkObjectExist_json(subDictnode))
					{
						break;
					}
                    tag = DICTOOL->getIntValue_json(subDictnode, "objecttag");
                    this->gateEdit(tag, subDictnode,rooxy);

				}
				

			}
			
        }


	}
{
    "entities": [
        {
            "entity": {
                "TapOpposite": 0, 
                "Interval": 0.95, 
                "BallNum": 1
            }
        }, 
        {
            "entity": {
                "TapOpposite": 0, 
                "Interval": 0.91, 
                "BallNum": 2
            }
        }, 
        {
            "entity": {
                "TapOpposite": 0, 
                "Interval": 0.95, 
                "BallNum": 3
            }
        }
    ]
}
void GameWorld::readJson()
{
	//json   
	rapidjson::Document _doc;
	bool bRet = false;
	ssize_t size = 0;
	unsigned char *pBytes = NULL;
	do {
		pBytes = cocos2d::CCFileUtils::sharedFileUtils()->getFileData("ball.json", "r", &size);
		CC_BREAK_IF(pBytes == NULL || strcmp((char*)pBytes, "") == 0);
		std::string load_str((const char*)pBytes, size);
		CC_SAFE_DELETE_ARRAY(pBytes);
		_doc.Parse<0>(load_str.c_str());
		CC_BREAK_IF(_doc.HasParseError());			
		//  json    

		if(!_doc.IsObject())
			return;

		//      
		if(!_doc.HasMember("entities"))
			return;

		//   []    ,       array,int,double,string
		const rapidjson::Value &pArray = _doc["entities"];

		//     
		if(!pArray.IsArray())
			return;

		for (rapidjson::SizeType i = 0; i < pArray.Size(); i++)
		{
			const rapidjson::Value &p = pArray[i];				
			if(p.HasMember("entity"))
			{
				const rapidjson::Value &valueEnt = p["entity"];
				if(valueEnt.HasMember("TapOpposite") && valueEnt.HasMember("Interval") && valueEnt.HasMember("BallNum"))
				{
					const rapidjson::Value &tapOpposite = valueEnt["TapOpposite"];
					int tapOp = tapOpposite.GetInt();      //  int 

					const rapidjson::Value &interval = valueEnt["Interval"];
					float inter = interval.GetDouble();  //  float,double 

					const rapidjson::Value &ballNum = valueEnt["BallNum"];
					int ball = ballNum.GetInt();      //  int 

					ballParam param;
					param.tapOp = tapOp;
					param.inter = inter;
					param.ballIndex = ball;
					m_ballParamVec.push_back(param);
				}
			}
			else
			{
				return;
			}

		}
		bRet = true;

	} while (0);
}

좋은 웹페이지 즐겨찾기