共分两个部分,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进行对象的嵌套封装的更多相关文章

  1. PYTHON-函数对象,嵌套,名称空间与作用域,闭包函数

    一 函数是第一类对象,即函数可以当作数据传递 1 可以被引用 2 可以当作参数传递 3 返回值可以是函数 3 可以当作容器类型的元素 def foo(): return len f = foo pri ...

  2. ElasticSearch入门 第六篇:复合数据类型——数组,对象和嵌套

    这是ElasticSearch 2.4 版本系列的第六篇: ElasticSearch入门 第一篇:Windows下安装ElasticSearch ElasticSearch入门 第二篇:集群配置 E ...

  3. Java-Runoob-面向对象:Java 封装

    ylbtech-Java-Runoob-面向对象:Java 封装 1.返回顶部 1. Java 封装 在面向对象程式设计方法中,封装(英语:Encapsulation)是指一种将抽象性函式接口的实现细 ...

  4. C# json反序列化 对象中嵌套数组 (转载) 可能会导致循环或多重级联路径。请指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。

    C# json反序列化 对象中嵌套数组 (转载)   看图: 这里可以看到是二层嵌套!!使用C#如何实现?? 思路:使用list集合实现 → 建立类 → list集合 → 微软的   Newtonso ...

  5. 回调函数 和 promise对象,及封装API接口

    1.回调函数:https://blog.csdn.net/baidu_32262373/article/details/54969696 注意:回调函数不一定需要用到 return.如果浏览器支持Pr ...

  6. 用类方法------>快速创建一个autorelease的对象,在封装的类方法内部

    在封装的类方法内部,也就是+ (id)personWithName:(NSString *)name andAge:(int)age内部: 创建了一个person对象,并且创建了一个person*类型 ...

  7. 对象(类)的封装方式(面向对象的js基本知识)

    上一个月一直忙于项目,没写过笔记,今天稍微空下来了一点 前几天在写项目的时候关于怎么去封装每一个组件的时候思考到几种方式,这里总结一下: 1.构造函数方式(类似java写类的方式):把所有的属性和方法 ...

  8. request对象多种方法封装表单数据

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, ...

  9. 10-Python入门学习-函数的对象与嵌套、名称空间与作用域、闭包函数

    一.函数的对象 函数是第一类对象,指的是函数名指向的值(函数)可以被当作数据去使用 def func():# func=函数的内地址 print('from func') print(func) ag ...

随机推荐

  1. apt-get常见错误——Unmet dependencies

    转自:http://blog.sina.com.cn/s/blog_4980828b0100zicn.html 安装错误:“E: Unmet dependencies.”原因:非正常停止apt-get ...

  2. Ubuntu14.04LTS系统QQ的安装:pidgin-lwqq

    本人是轻度聊天工具使用者(大言不惭是轻度,偷笑),发现输入法到博主也有解决linux下QQ的解决方法,一并抄过来,有需要,请联系原作者 参考链接:http://www.cnblogs.com/zhj5 ...

  3. XML 文件解析

    1.XML文件 <Data> <Movie id="1"> <title>good lucky to you</title> < ...

  4. HTML概况性介绍

    HTML(HyperText Markup Language)汉语的意思是:超文本标记语言. ”超文本”是指.html页面内不仅仅可以包含文字,还可以包含图片.链接,甚至音乐.程序等非文字元素. “标 ...

  5. Codeforces Round #354 (Div. 2)-A

    A. Nicholas and Permutation 题目链接:http://codeforces.com/contest/676/problem/A Nicholas has an array a ...

  6. iOS10 UI教程基础窗口的内容与设置起始窗口

    iOS10 UI教程基础窗口的内容与设置起始窗口 iOS10 UI教程基础窗口的内容与设置起始窗口,本章我们从iOS10开发中UI的基础知识开始讲解,其中包括了窗口.视图以及UI层次结构和Views的 ...

  7. 浩瀚移动POS收银开单扫描解决方案PDA仓储系统,无线批发,移动批发,无线POS,无线销售APP-车销管理PDA

    适用范围 各种业态的批发商铺.批发市场.订货会.展销会.配送中心仓库…… 产品简介 随着移动技术与智能PDA设备的迅猛发展,中国已经跨步进入移动信息化社会.移动商务是移动信息社会的重要载体与形式,它开 ...

  8. BZOJ 1189 [HNOI2007]紧急疏散evacuate

    Description 发生了火警,所有人员需要紧急疏散!假设每个房间是一个N M的矩形区域.每个格子如果是'.',那么表示这是一块空地:如果是'X',那么表示这是一面墙,如果是'D',那么表示这是一 ...

  9. 去除手机端a标签等按下去背景色

    a,button,input,textarea,label,i,em{/*highlight*/ -webkit-tap-highlight-color: rgba(255,0,0,0); borde ...

  10. bash脚本中的普通数组和关联数组

    1. 普通数组 bash支持一维数组(不支持多维数组),并且没有限定数组的大小.类似与C语言,数组元素的下标由0开始编号.获取数组中的元素要利用下标,下标可以是整数或算术表达式,其值应大于或等于0. ...