使用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 ...
随机推荐
- poj 3264 【线段树】
此题为入门级线段树 题意:给定Q(1<=Q<=200000)个数A1A2…AQ,多次求任一区间Ai-Aj中最大数和最小数的差 #include<algorithm> #incl ...
- Java Web 项目获取运行时路径 classpath
假设资源文件放在maven工程的 src/main/resources 资源文件夹下,源码文件放在 src/main/java/下, 那么java文件夹和resources文件夹在运行时就是class ...
- Eclipse调试方法及快捷键
基本操作 断点,breakpoint: F5键与F6键均为单步调试: F5是step into,也就是进入本行代码中执行,跳入 F6是step over,跳过,也就是执行本行代码,跳到下一行 F7是跳 ...
- android如何实现文件按时间先后顺序排列显示
<span style="font-size:18px;">File[] files =parentFile.listFiles(fileFilter);//通过fil ...
- strust.xml
使用strust2框架,实现跳转,请求对应路径 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTY ...
- CSS3动画属性animation的用法
转载: 赞生博客 高端订制web开发工作组 » CSS3动画属性animation的用法 CSS3提供了一个令人心动的动画属性:animation,尽管利用animation做出来的动画没有flash ...
- Android优化
ListView的优化 复用convertview , 历史的view对象 减少子孩子查询的次数 viewholder 异步加载数据(把图片缓存) 条目多时分页加载数据 加载时显示进度条让用户等待 I ...
- psql-04数据类型(2)
复合类型 PostgreSQL中可以如C语言中的结构体一样定义一个复合类型; 创建 create type person as ( name text, age int, sex boolean ); ...
- HIVE 创建外部分区表--利用HUE不能创建外部表
Create EXTERNAL table obd_data_2( imei string, ts timestamp, fuel_instant float, gps_speed float, gp ...
- MFC 打开其他程序
WinExec("程序路径",SW_SHOW);