看其他解析json的,没有满足自己的,贴一个自己的:
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include "cJSON.h"
int _tmain(int argc, char* argv[])
{
/*{
"ab": "cd",
"ef" : {
"ab": "cd",
"ef" : "gh"
}
}*/
std::string str = "{\"ab\": \"cd\",\"ef\" : \"{\\\"ab\\\": \\\"cd\\\",\\\"ef\\\": \\\"gh\\\"}\"}";
cJSON *strJson = cJSON_Parse(str.c_str());
int iSize = cJSON_GetArraySize(strJson);
for (int iCnt = 0; iCnt < iSize; iCnt++)
{
cJSON * pSub = cJSON_GetArrayItem(strJson, iCnt);
if (NULL == pSub)
{
continue;
}
std::string key = pSub->string;
std::string value = pSub->valuestring;
printf("%s:%s\n", key.c_str(),value.c_str());
}
cJSON_Delete(strJson);
return 0;
}