Json3

来源:互联网 发布:淘宝自定义代码 编辑:程序博客网 时间:2024/06/03 14:15

voidInputLayer::readJson()

{

    rapidjson::Document _doc;

    bool bRet =false;

ssize_t size =0;

unsignedchar *pBytes = NULL;

    do {

pBytes =FileUtils::getInstance()->getFileData("protocal.json","r", &size);

CC_BREAK_IF(pBytes ==NULL || strcmp((char*)pBytes,"") == 0);

std::string load_str((constchar*)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

constrapidjson::Value &pArray = _doc["entities"];

        

//是否是数组

if(!pArray.IsArray())

return;

        

for (rapidjson::SizeType i =0; i < pArray.Size(); i++)

{

constrapidjson::Value &p = pArray[i];

if(p.HasMember("entity"))

{

constrapidjson::Value &valueEnt = p["entity"];

if(valueEnt.HasMember("TapOpposite") && valueEnt.HasMember("Interval") && valueEnt.HasMember("BallNum"))

{

constrapidjson::Value &tapOpposite = valueEnt["TapOpposite"];

int tapOp = tapOpposite.GetInt();     //得到int

                    

constrapidjson::Value &interval = valueEnt["Interval"];

float inter = interval.GetDouble(); //得到float,double

                    

constrapidjson::Value &ballNum = valueEnt["BallNum"];

int ball = ballNum.GetInt();     //得到int

                

                    

                    CCLOG("tapOp==%d, inter==%f, ball==%d", tapOp, inter, ball);

                    

}

}

else

{

return;

}

            

}

bRet =true;

        

}while (0);

}





{
    "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
            }
        }
    ]
}

0 0
原创粉丝点击