LibJson数据解析方法
void HelloWorld::ParseJSON(JSONNode *n,CCMutableArray<CCObject *> *array,CCMutableDictionary<std::string,CCObject *> *dic,CCMutableDictionary<std::string,CCObject *> *dataDicLast)
{
if(n==NULL)
{
printf("擦 什么都沒(méi)有你簡(jiǎn)析個(gè)屁了呀!滾蛋\n");
}
JSONNode::iterator iterator=n->begin();
while (iterator!=n->end())
{
//-------------------獲取key
json_string stringKeyJson=iterator->name();
const string stdKeyString=to_std_string(stringKeyJson);
//printf("key====%s",stdKeyString.c_str());
//-------------------獲取Value
const char stringValueJson=iterator->type();
//printf("type===%d\n",stringValueJson);
if(iterator->type()==JSON_ARRAY && stdKeyString!="")
{
CCMutableArray<CCObject *> *tempArray=new CCMutableArray<CCObject *>;
ParseJSON(&(*iterator),tempArray,NULL,NULL);
dataDicLast->setObject(tempArray, stdKeyString.c_str());
tempArray->release();
}
else if(iterator->type()==JSON_ARRAY)
{
CCMutableArray<CCObject *> *tempArray=new CCMutableArray<CCObject *>;
ParseJSON(&(*iterator),tempArray,NULL,NULL);
array->addObject(tempArray);
tempArray->release();
}
else if(iterator->type()==JSON_STRING && stdKeyString!="")
{
json_string valueJString=iterator->as_string();
std::string ValueCstring=to_std_string(valueJString);
CCString *valueString=new CCString(ValueCstring.c_str());
dataDicLast->setObject(valueString, stdKeyString.c_str());
valueString->release();
}
else if(iterator->type()==JSON_STRING)
{
json_string valueJString=iterator->as_string();
std::string ValueCstring=to_std_string(valueJString);
CCString *valueString=new CCString(ValueCstring.c_str());
array->addObject(valueString);
valueString->release();
}
else if(iterator->type()==JSON_NODE && stdKeyString!="")
{
CCMutableDictionary<std::string,CCObject *> *tempDic=new CCMutableDictionary<std::string,CCObject *>;
ParseJSON(&(*iterator),NULL,NULL,tempDic);
dataDicLast->setObject(tempDic, stdKeyString.c_str());
tempDic->release();
}
else
{
CCMutableDictionary<std::string,CCObject *> *tempDic=new CCMutableDictionary<std::string,CCObject *>;
ParseJSON(&(*iterator),NULL,NULL,tempDic);
dic->setObject(tempDic, stdKeyString.c_str());
tempDic->release();
}
iterator++;
}
}
轉(zhuǎn)載于:https://blog.51cto.com/wangxu254/1313838
總結(jié)
以上是生活随笔為你收集整理的LibJson数据解析方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。