使用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 ...
随机推荐
- Solr入门之(1)前言与概述
一.前言:为何选择Solr 由于搜索引擎功能在门户社区中对提高用户体验有着重在门户社区中涉及大量需要搜索引擎的功能需求,目前在实现搜索引擎的方案上有几种方案可供选择: 1. 基于Lucene自己进行封 ...
- oracle pctfree和pctused详解
一.建立表时候,注意PCTFREE参数的作用 PCTFREE:为一个块保留的空间百分比,表示数据块在什么情况下可以被insert,默认是10,表示当数据块的可用空间低于10%后,就不可以被insert ...
- Sizeof与Strlen的区别与联系
转自:http://www.cnblogs.com/carekee/articles/1630789.html 一.sizeof sizeof(...)是运算符,在头文件中typedef为uns ...
- 第十九篇:提高SOUI应用程序渲染性能的三种武器
SOUI是一套100%开源的基于DirectUI的客户端开发框架. 基于DirectUI设计的UI虽然UI呈现的效果可以很炫,但是相对于传统的win32应用程序中每个控件一个窗口句柄的形式,渲染效率是 ...
- IIS 内部运行机制
ASP.NET是一个非常强大的构建Web应用的平台,它提供了极大的灵活性和能力以致于可以用它来构建所有类型的Web应用. 绝大多数的人只熟悉高层的框架如: WebForms 和 WebServices ...
- 在Salesforce中编写Unit Test
Unit Test 也是一个 Class 文件,所以在创建 Unit Test 的时候要选择 Test Class 类型来创建,请看如下截图(在Eclipse中): 编写 Unit Test 基本流程 ...
- Comet:基于 HTTP 长连接的“服务器推”技术解析
原文链接:http://www.cnblogs.com/deepleo/p/Comet.html 一.背景介绍 传统web请求,是显式的向服务器发送http Request,拿到Response后显示 ...
- 智能车学习(十八)——电机学习
一.C车电机选择 1.摘要: 因为C车模在四轮车的优势是有两个电机,可以进行主动差速,劣势是电机太弱了....所以如何选择电机,就是个钱的问题了,电机多一点,就比较好选,但是C车电机跑多了就 ...
- open文件操作
open()做文件操作的就是他1.打开文件#f=open("db","r")#只读#f-open("db","w")#只 ...
- Android打包的那些事
使用gradle打包apk已经成为当前主流趋势,我也在这个过程中经历了各种需求,并不断结合gradle新的支持,一一改进.在此,把这些相关的东西记录,做一总结. 1. 替换AndroidManifes ...