cjson 解析 字符串

来源:互联网 发布:想做网络客服 编辑:程序博客网 时间:2024/05/17 23:07

看其他解析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;}


0 0
原创粉丝点击