使用cjson进行对象的嵌套封装
共分两个部分,1)创建json、2)解析json
1)创建嵌套json的代码
char * makeJson()
{
cJSON * pRoot = NULL;
cJSON * pSub_1 = NULL;
cJSON * pSub_2 = NULL; if((pRoot = cJSON_CreateObject()) == NULL)
{
return NULL;
}
if((pSub_1 = cJSON_CreateObject()) == NULL)
{
return NULL;
}
if((pSub_2 = cJSON_CreateObject()) == NULL)
{
return NULL;
}
cJSON_AddStringToObject(pSub_2, "cStr", "ccccccc"); cJSON_AddStringToObject(pSub_1, "bStr", "bbbbbbb");
cJSON_AddItemToObject(pSub_1, "subobject_2", pSub_2); cJSON_AddStringToObject(pRoot, "aStr", "aaaaaaa");
cJSON_AddItemToObject(pRoot, "subobject_1", pSub_1);
cJSON_AddStringToObject(pRoot, "xStr", "xxxxxxx"); //cJSON_PrintUnformatted : make json string for Unformatted
//char * pJson = cJSON_PrintUnformatted(pRoot); char * pJson = cJSON_Print(pRoot);
if(NULL == pJson)
{
cJSON_Delete(pRoot);
return NULL;
}
return pJson;
}
2)解析json的代码
int parseJson(const char * pJson)
{
if(NULL == pJson)
{
return ;
}
cJSON * pRoot = cJSON_Parse(pJson);
if(NULL == pRoot)
{
return ;
}
cJSON * pSub_1 = cJSON_GetObjectItem(pRoot, "aStr");
if(NULL == pSub_1)
{
cJSON_Delete(pRoot);
return ;
}
printf("get aStr : [%s]\n", pSub_1->valuestring);
pSub_1 = cJSON_GetObjectItem(pRoot, "xStr");
if(NULL == pSub_1)
{
cJSON_Delete(pRoot);
return ;
}
printf("get xStr : [%s]\n", pSub_1->valuestring);
pSub_1 = cJSON_GetObjectItem(pRoot, "subobject_1");
if(NULL == pSub_1)
{
cJSON_Delete(pRoot);
return ;
}
printf("get Sub Obj 1\n");
cJSON * pSub_2 = cJSON_GetObjectItem(pSub_1, "bStr");
if(NULL == pSub_2)
{
cJSON_Delete(pRoot);
return ;
}
printf("get bStr : [%s]\n", pSub_2->valuestring);
pSub_2 = cJSON_GetObjectItem(pSub_1, "subobject_2");
if(NULL == pSub_2)
{
cJSON_Delete(pRoot);
return ;
}
printf("get Obj 2\n");
cJSON * pStr = cJSON_GetObjectItem(pSub_2, "cStr");
if(NULL == pStr)
{
cJSON_Delete(pRoot);
return ;
}
printf("get cStr : [%s]\n", pStr->valuestring); cJSON_Delete(pRoot);
return ;
}
3)主函数
int main()
{
char * pJson = makeJson();
printf("JSON:\n%s\n", pJson);
parseJson(pJson);
free(pJson); return ;
}
完整的代码请打开下面的代码或到百度网盘下载 http://pan.baidu.com/s/1pJ7KZSR
#include <stdio.h>
#include "cJSON.h" char * makeJson()
{
cJSON * pRoot = NULL;
cJSON * pSub_1 = NULL;
cJSON * pSub_2 = NULL; if((pRoot = cJSON_CreateObject()) == NULL)
{
return NULL;
}
if((pSub_1 = cJSON_CreateObject()) == NULL)
{
return NULL;
}
if((pSub_2 = cJSON_CreateObject()) == NULL)
{
return NULL;
}
cJSON_AddStringToObject(pSub_2, "cStr", "ccccccc"); cJSON_AddStringToObject(pSub_1, "bStr", "bbbbbbb");
cJSON_AddItemToObject(pSub_1, "subobject_2", pSub_2); cJSON_AddStringToObject(pRoot, "aStr", "aaaaaaa");
cJSON_AddItemToObject(pRoot, "subobject_1", pSub_1);
cJSON_AddStringToObject(pRoot, "xStr", "xxxxxxx"); //cJSON_PrintUnformatted : make json string for Unformatted
//char * pJson = cJSON_PrintUnformatted(pRoot); char * pJson = cJSON_Print(pRoot);
if(NULL == pJson)
{
cJSON_Delete(pRoot);
return NULL;
}
return pJson;
} int parseJson(const char * pJson)
{
if(NULL == pJson)
{
return ;
}
cJSON * pRoot = cJSON_Parse(pJson);
if(NULL == pRoot)
{
return ;
}
cJSON * pSub_1 = cJSON_GetObjectItem(pRoot, "aStr");
if(NULL == pSub_1)
{
cJSON_Delete(pRoot);
return ;
}
printf("get aStr : [%s]\n", pSub_1->valuestring);
pSub_1 = cJSON_GetObjectItem(pRoot, "xStr");
if(NULL == pSub_1)
{
cJSON_Delete(pRoot);
return ;
}
printf("get xStr : [%s]\n", pSub_1->valuestring);
pSub_1 = cJSON_GetObjectItem(pRoot, "subobject_1");
if(NULL == pSub_1)
{
cJSON_Delete(pRoot);
return ;
}
printf("get Sub Obj 1\n");
cJSON * pSub_2 = cJSON_GetObjectItem(pSub_1, "bStr");
if(NULL == pSub_2)
{
cJSON_Delete(pRoot);
return ;
}
printf("get bStr : [%s]\n", pSub_2->valuestring);
pSub_2 = cJSON_GetObjectItem(pSub_1, "subobject_2");
if(NULL == pSub_2)
{
cJSON_Delete(pRoot);
return ;
}
printf("get Obj 2\n");
cJSON * pStr = cJSON_GetObjectItem(pSub_2, "cStr");
if(NULL == pStr)
{
cJSON_Delete(pRoot);
return ;
}
printf("get cStr : [%s]\n", pStr->valuestring); cJSON_Delete(pRoot);
return ;
} int main()
{
char * pJson = makeJson();
printf("JSON:\n%s\n", pJson);
parseJson(pJson);
free(pJson); return ;
}
编译
$ gcc -o nestcjson nestcjson.c cjson.c -lm
注:编译时链接的库 -lm 是数学库,不加此库时 gcc 返回错误,错误代码如下
$ gcc -o nestcjson nestcjson.c cjson.c
/tmp/ccugp95L.o: In function `parse_number':
cjson.c:(.text+0x402): undefined reference to `pow'
/tmp/ccugp95L.o: In function `print_number':
cjson.c:(.text+0x512): undefined reference to `floor'
collect2: ld 返回 1
运行
$ ./nestcjson
JSON:
{
"aStr": "aaaaaaa",
"subobject_1": {
"bStr": "bbbbbbb",
"subobject_2": {
"cStr": "ccccccc"
}
},
"xStr": "xxxxxxx"
}
get aStr : [aaaaaaa]
get xStr : [xxxxxxx]
get Sub Obj
get bStr : [bbbbbbb]
get Obj
get cStr : [ccccccc]
作者:风波
mail : fengbohello@qq.com
使用cjson进行对象的嵌套封装的更多相关文章
- PYTHON-函数对象,嵌套,名称空间与作用域,闭包函数
一 函数是第一类对象,即函数可以当作数据传递 1 可以被引用 2 可以当作参数传递 3 返回值可以是函数 3 可以当作容器类型的元素 def foo(): return len f = foo pri ...
- ElasticSearch入门 第六篇:复合数据类型——数组,对象和嵌套
这是ElasticSearch 2.4 版本系列的第六篇: ElasticSearch入门 第一篇:Windows下安装ElasticSearch ElasticSearch入门 第二篇:集群配置 E ...
- Java-Runoob-面向对象:Java 封装
ylbtech-Java-Runoob-面向对象:Java 封装 1.返回顶部 1. Java 封装 在面向对象程式设计方法中,封装(英语:Encapsulation)是指一种将抽象性函式接口的实现细 ...
- C# json反序列化 对象中嵌套数组 (转载) 可能会导致循环或多重级联路径。请指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。
C# json反序列化 对象中嵌套数组 (转载) 看图: 这里可以看到是二层嵌套!!使用C#如何实现?? 思路:使用list集合实现 → 建立类 → list集合 → 微软的 Newtonso ...
- 回调函数 和 promise对象,及封装API接口
1.回调函数:https://blog.csdn.net/baidu_32262373/article/details/54969696 注意:回调函数不一定需要用到 return.如果浏览器支持Pr ...
- 用类方法------>快速创建一个autorelease的对象,在封装的类方法内部
在封装的类方法内部,也就是+ (id)personWithName:(NSString *)name andAge:(int)age内部: 创建了一个person对象,并且创建了一个person*类型 ...
- 对象(类)的封装方式(面向对象的js基本知识)
上一个月一直忙于项目,没写过笔记,今天稍微空下来了一点 前几天在写项目的时候关于怎么去封装每一个组件的时候思考到几种方式,这里总结一下: 1.构造函数方式(类似java写类的方式):把所有的属性和方法 ...
- request对象多种方法封装表单数据
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, ...
- 10-Python入门学习-函数的对象与嵌套、名称空间与作用域、闭包函数
一.函数的对象 函数是第一类对象,指的是函数名指向的值(函数)可以被当作数据去使用 def func():# func=函数的内地址 print('from func') print(func) ag ...
随机推荐
- css3 妙味
css3 属性 <!DOCTYPE html> <html> <head lang="en"> <meta charset="U ...
- jQuery实现无限加载瀑布流特效
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- WPF中加载高分辨率图片性能优化
在最近的项目中,遇到一个关于WPF中同时加载多张图片时,内存占用非常高的问题. 问题背景: 在一个ListView中同时加载多张图片,注意:我们需要加载的图片分辨率非常高. 代码: XAML: < ...
- hdu 4050 2011北京赛区网络赛K 概率dp ***
题目:给出1-n连续的方格,从0开始,每一个格子有4个状态,左右脚交替,向右跳,而且每一步的步长必须在给定的区间之内.当跳出n个格子或者没有格子可以跳的时候就结束了,求出游戏的期望步数 0:表示不能到 ...
- [Eclipse] Eclipse字体问题解决
背景: Eclipse的字体总感觉有点问题,其中中文字体太小,不方便查看,今天网上搜索了一下,解决了问题,记录下来: 解决办法: Window --> Preferences --> Ge ...
- Build Instructions (Windows) – The Chromium Projects
转自:http://121.199.54.6/wordpress/?p=1156 原始地址:http://www.chromium.org/developers/how-tos/build-instr ...
- Java Data Type
官方文档:http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html 转载地址:http://blog.csdn.n ...
- 【rqnoj378】 约会计划
题目描述 cc是个超级帅哥,口才又好,rp极高(这句话似乎降rp),又非常的幽默,所以很多mm都跟他关系不错.然而,最关键的是,cc能够很好的调解各各妹妹间的关系.mm之间的关系及其复杂,cc必须严格 ...
- ios摇一摇截屏代码
#import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...
- Java学习笔记(五)——数组
一.数组使用方法 1. 声明数组 语法: 数据类型[ ] 数组名: 或者 数据类型 数组名[ ]: 其中,数组名可以是任意合法的变量名 2. 分配空间 简单地说,就是指定数组中最多可存储多少个元素 ...