您的位置:首页 > Web前端 > JavaScript

cjson 解析 字符串

2015-05-29 17:23 549 查看
看其他解析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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: