使用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 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

#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
作 者:fengbohello
个人网站:http://www.fengbohello.top/
E-mail : fengbohello@foxmail.com
欢迎转载,转载请注明作者和出处。
因作者水平有限,不免出现遗漏和错误。希望热心的同学能够帮我指出来,我会尽快修改。愿大家共同进步,阿里嘎多~
标签:
JSON
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?