使用cjson进行对象的嵌套封装
2023-09-11 14:18:54 时间
共分两个部分,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 1; } cJSON * pRoot = cJSON_Parse(pJson); if(NULL == pRoot) { return 2; } cJSON * pSub_1 = cJSON_GetObjectItem(pRoot, "aStr"); if(NULL == pSub_1) { cJSON_Delete(pRoot); return 3; } printf("get aStr : [%s]\n", pSub_1->valuestring); pSub_1 = cJSON_GetObjectItem(pRoot, "xStr"); if(NULL == pSub_1) { cJSON_Delete(pRoot); return 4; } printf("get xStr : [%s]\n", pSub_1->valuestring); pSub_1 = cJSON_GetObjectItem(pRoot, "subobject_1"); if(NULL == pSub_1) { cJSON_Delete(pRoot); return 5; } printf("get Sub Obj 1\n"); cJSON * pSub_2 = cJSON_GetObjectItem(pSub_1, "bStr"); if(NULL == pSub_2) { cJSON_Delete(pRoot); return 6; } printf("get bStr : [%s]\n", pSub_2->valuestring); pSub_2 = cJSON_GetObjectItem(pSub_1, "subobject_2"); if(NULL == pSub_2) { cJSON_Delete(pRoot); return 7; } printf("get Obj 2\n"); cJSON * pStr = cJSON_GetObjectItem(pSub_2, "cStr"); if(NULL == pStr) { cJSON_Delete(pRoot); return 8; } printf("get cStr : [%s]\n", pStr->valuestring); cJSON_Delete(pRoot); return 0; }
3)主函数
int main() { char * pJson = makeJson(); printf("JSON:\n%s\n", pJson); parseJson(pJson); free(pJson); return 0; }
完整的代码请打开下面的代码或到百度网盘下载 http://pan.baidu.com/s/1pJ7KZSR
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#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 1; } cJSON * pRoot = cJSON_Parse(pJson); if(NULL == pRoot) { return 2; } cJSON * pSub_1 = cJSON_GetObjectItem(pRoot, "aStr"); if(NULL == pSub_1) { cJSON_Delete(pRoot); return 3; } printf("get aStr : [%s]\n", pSub_1->valuestring); pSub_1 = cJSON_GetObjectItem(pRoot, "xStr"); if(NULL == pSub_1) { cJSON_Delete(pRoot); return 4; } printf("get xStr : [%s]\n", pSub_1->valuestring); pSub_1 = cJSON_GetObjectItem(pRoot, "subobject_1"); if(NULL == pSub_1) { cJSON_Delete(pRoot); return 5; } printf("get Sub Obj 1\n"); cJSON * pSub_2 = cJSON_GetObjectItem(pSub_1, "bStr"); if(NULL == pSub_2) { cJSON_Delete(pRoot); return 6; } printf("get bStr : [%s]\n", pSub_2->valuestring); pSub_2 = cJSON_GetObjectItem(pSub_1, "subobject_2"); if(NULL == pSub_2) { cJSON_Delete(pRoot); return 7; } printf("get Obj 2\n"); cJSON * pStr = cJSON_GetObjectItem(pSub_2, "cStr"); if(NULL == pStr) { cJSON_Delete(pRoot); return 8; } printf("get cStr : [%s]\n", pStr->valuestring); cJSON_Delete(pRoot); return 0; } int main() { char * pJson = makeJson(); printf("JSON:\n%s\n", pJson); parseJson(pJson); free(pJson); return 0; }
编译
$ 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 1 get bStr : [bbbbbbb] get Obj 2 get cStr : [ccccccc]
作者:风波
mail : fengbohello@qq.com
相关文章
- hibernate5(8)操纵对象入门[3]操控对象封装方法
- 纯php分词封装的类
- 第一百六十节,封装库--JavaScript,ajax注册表单到数据库
- 第一百四十一节,JavaScript,封装库--DOM加载
- 【COCOS2D-X(1.X 2.X) JSON(CPP版)及新加字体库篇】在COCOS2DX引擎中封装、解析JSON(CPP版)数据以及添加自定义字体库
- PHP JSON 封装
- C# 视频监控系列(6):服务器端——封装API(上) [HikServer.dll]
- POI读取Excel 初步支持对象封装功能
- c++模板学习12之通用数组类模板案例封装
- C# SQLite数据库 访问封装类
- Atitit 封装的艺术 目录 1.1. 规范是不暴露特有的api1 1.2. 方便理解,提升可读性1 1.3. Atitit 提升可读性 数据结构特殊化专用api 比较通用的对象
- Python语言学习:Python语言学习之面向对象编程OO(继承&封装&多态)/类&方法/装饰器的简介、案例应用之详细攻略
- 图片视频组件封装
- 学生管理系统----当然封装类型
- JAVA会将所有的错误封装成为一个对象,其根本父类为Throwable
- proteus仿真软件中芯片的命名规则与封装方法(详细版)
- 【JS高级】js面向对象三大特性之封装—如何创建对象_05