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;
}
#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;
}
相关文章推荐
- JavaScript 浏览器 UserAgent
- JavaScript UserAgent判断---摘自Professional JavaScript for Web Developers 3rd
- js 小技巧
- JSP学习笔记(三):解决JSP中文乱码问题
- jstl标签详解
- json读取数据的几种方式
- 201505291704_《JavaScript的很‘二’的用法,但对理解js很有用!!》
- js正则表达式校验
- Js实现类似Excel工作表用方向键切换输入框焦点
- JSONResult引用某博客
- js中数据移除元素
- 键盘事件与JS Filter
- JSP相对路径与绝对路径探秘
- ExtJS 4.2 02 ExtJs 基础
- JavaScript closure
- JSON对象和字符串之间的相互转换
- js实现可兼容IE、FF、Chrome、Opera及Safari的音乐播放器
- js学习-函数
- js获取鼠标移动位置
- js发送邮件确定email地址