当前位置: 代码迷 >> 综合 >> cjson 解析 字符串
  详细解决方案

cjson 解析 字符串

热度:1   发布时间:2023-12-15 22:57:55.0

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


  相关解决方案