c语言解析json代码举例,CJSON 实例
cJSON: 一个用c写的一个简单好用的JSON解析器
cJSON库
1、json的数据结构
c语言中json数据是采用链表存储的
typedef struct cJSON {
struct cJSON *next,*prev;// 数组 对象数据中用到
struct cJSON *child;// 数组 和对象中指向子数组对象或值
int type;// 元素的类型,如是对象还是数组
char *valuestring;// 如果是字符串
int valueint; // 如果是数值
double valuedouble;// 如果类型是cJSON_Number
char *string;// The item's name string,if this item is the child of,or is in the list of subitems of an object.
} cJSON;
cJSON使用
{
"name": "Jack (\"Bee\") Nimble",
"format": {
"type": "rect",
"width": 1920,
"height": 1080,
"interlace": false,
"frame rate": 24
}
}
1、字符串解析成json结构体
1):字符串解析成json结构体。
cJSON *root = cJSON_Parse(my_json_string);
2):获取某个元素
cJSON *format = cJSON_GetObjectItem(root,"format");
int framerate = cJSON_GetObjectItem(format,"frame rate")->valueint;
int framerate = cJSON_GetObjectItem(format,"frame rate")->valueint;
3):讲json结构体转换成字符串
char *rendered=cJSON_Print(root);
4):删除
cJSON_Delete(root);
2:构建一个json结构体
cJSON *root,*fmt;
root=cJSON_CreateObject();
cJSON_AddItemToObject(root,"name",cJSON_CreateString("Jack (\"Bee\") Nimble"));
cJSON_AddItemToObject(root,"format",fmt=cJSON_CreateObject());
cJSON_AddStringToObject(fmt,"type","rect");
cJSON_AddNumberToObject(fmt,"width",1920);
cJSON_AddNumberToObject(fmt,"height",1080);
cJSON_AddFalseToObject (fmt,"interlace");
cJSON_AddNumberToObject(fmt,"frame rate",24)
out =cJSON_Print(root);
printf("%s\n",out);
cJSON_Delete(root);
free(out);
本文用代码简单介绍cjson的使用方法,1)创建json,从json中获取数据。2)创建json数组和解析json数组
1、创建json,从json中获取数据
1 #include
2 #include "cJSON.h"
3
4 char *makeJson()5 {6 cJSON * pJsonRoot =NULL;7
8 pJsonRoot =cJSON_CreateObject();9 if(NULL ==pJsonRoot)10 {11 //error happend here
12 returnNULL;13 }14 cJSON_AddStringToObject(pJsonRoot,"hello","hello world");15 cJSON_AddNumberToObject(pJsonRoot,"number",10010);16 cJSON_AddBoolToObject(pJsonRoot,"bool",1);17 cJSON * pSubJson =NULL;18 pSubJson =cJSON_CreateObject();19 if(NULL ==pSubJson)20 {21 //create object faild,exit
22 cJSON_Delete(pJsonRoot);23 returnNULL;24 }25 cJSON_AddStringToObject(pSubJson,"subjsonobj","a sub json string");26 cJSON_AddItemToObject(pJsonRoot,"subobj",pSubJson);27
28 char * p =cJSON_Print(pJsonRoot);29 //else use :30 //char * p = cJSON_PrintUnformatted(pJsonRoot);
31 if(NULL ==p)32 {33 //convert json list to string faild,exit34 //because sub json pSubJson han been add to pJsonRoot,so just delete pJsonRoot,if you also delete pSubJson,it will coredump,and error is : double free
35 cJSON_Delete(pJsonRoot);36 returnNULL;37 }38 //free(p);
39
40 cJSON_Delete(pJsonRoot);41
42 returnp;43 }44
45 void parseJson(char *pMsg)46 {47 if(NULL ==pMsg)48 {49 return;50 }51 cJSON * pJson =cJSON_Parse(pMsg);52 if(NULL ==pJson)53 {54 //parse faild,return
55 return;56 }57
58 //get string from json
59 cJSON * pSub = cJSON_GetObjectItem(pJson,"hello");60 if(NULL ==pSub)61 {62 //get object named "hello" faild
63 }64 printf("obj_1 : %s\n",pSub->valuestring);65
66 //get number from json
67 pSub = cJSON_GetObjectItem(pJson,"number");68 if(NULL ==pSub)69 {70 //get number from json faild
71 }72 printf("obj_2 : %d\n",pSub->valueint);73
74 //get bool from json
75 pSub = cJSON_GetObjectItem(pJson,"bool");76 if(NULL ==pSub)77 {78 //get bool from json faild
79 }80 printf("obj_3 : %d\n",pSub->valueint);81
82 //get sub object
83 pSub = cJSON_GetObjectItem(pJson,"subobj");84 if(NULL ==pSub)85 {86 //get sub object faild
87 }88 cJSON * pSubSub = cJSON_GetObjectItem(pSub,"subjsonobj");89 if(NULL ==pSubSub)90 {91 //get object from subject object faild
92 }93 printf("sub_obj_1 : %s\n",pSubSub->valuestring);94
95 cJSON_Delete(pJson);96 }97
98 intmain()99 {100 char * p =makeJson();101 if(NULL ==p)102 {103 return 0;104 }105 printf("%s\n",p);106 parseJson(p);107 free(p); //千万不要忘记释放内存呀,cJSON_Print()函数或者cJSON_PrintUnformatted()产生的内存,使用free(char *)进行释放
108 return 0;109 }
centos下编译通过,运行结果如下
1 {2 "hello": "hello world",3 "number": 10010,4 "bool": true,5 "subobj": {6 "subjsonobj": "a sub json string"
7 }8 }9 obj_1 : hello world10 obj_2 : 10010
11 obj_3 : 1
12 sub_obj_1 : a sub json string
代码解释如下:
CJSON在内存中的存储方式是用链表进行存储的,所以在进行操作的时候,我们可见的部分全部是用指针进行操作的。
第8行新建一个JSON项目。
第14、15、16行分别添加了字符串、数字和bool变量。
第18行新建一个JSON项目:pSubJson。
第25行在新建的pSubJson项目上添加字符串。
第26行把我们的新项目添加到最初的项目pJsonRoot上。
第28行把CJSON的内存的存储的数据转换为字符串格式。
结果分析:
第1到8行为创建的JSON字符串
第9到12行为从JSON解析得到的数据
2、创建json数组和解析json数组
1 //创建数组,数组值是另一个JSON的item,这里使用数字作为演示
2 char * makeArray(intiSize)3 {4 cJSON * root =cJSON_CreateArray();5 if(NULL ==root)6 {7 printf("create json array faild\n");8 returnNULL;9 }10 int i = 0;11
12 for(i = 0; i < iSize; i++)13 {14 cJSON_AddNumberToObject(root,"hehe",i);15 }16 char * out =cJSON_Print(root);17 cJSON_Delete(root);18
19 return out;20 }21
22 //解析刚刚的CJSON数组
23 void parseArray(char *pJson)24 {25 if(NULL ==pJson)26 {27 return;28 }29 cJSON * root =NULL;30 if((root = cJSON_Parse(pJson)) ==NULL)31 {32 return;33 }34 int iSize =cJSON_GetArraySize(root);35 for(int iCnt = 0; iCnt < iSize; iCnt++)36 {37 cJSON * pSub =cJSON_GetArrayItem(root,iCnt);38 if(NULL ==pSub)39 {40 continue;41 }42 int iValue = pSub->valueint;43 printf("value[%2d] : [%d]\n",iCnt,iValue);44 }45 cJSON_Delete(root);46 return;47 }
输出结果
1)创建JSON数组
[0,1,2,3,4,5,6,7,8,9]
2)解析数组的输出结果
value[ 0] : [0]
value[1] : [1]
value[2] : [2]
value[3] : [3]
value[4] : [4]
value[5] : [5]
value[6] : [6]
value[7] : [7]
value[8] : [8]
value[9] : [9]
c语言解析json代码举例,CJSON 实例相关推荐
- python自动解析json_Python语言解析JSON详解
本文主要向大家介绍了Python语言解析JSON详解,通过具体的内容向大家展示,希望对大家学习Python语言有所帮助. JSON 函数使用 JSON 函数需要导入 json 库:import jso ...
- c语言解析json报文源码,GitHub - faycheng/cJSON: cJson源码和源码分析
cJSON官方文档翻译 项目预期目标 文档翻译 函数列表 项目架构 源码注释 解析流程 快速使用 ###版权说明 Copyright (c) 2009 Dave Gamble Permission i ...
- python抓包代码_Python抓包并解析json爬虫的完整实例代码
Python抓包并解析json爬虫FwA免费资源网 在使用Python爬虫的时候,通过抓包url,打开url可能会遇见以下类似网址,打开后会出现类似这样的界面,无法继续进行爬虫:FwA免费资源网 Fw ...
- python 抓包解析数据_Python抓包并解析json爬虫的完整实例代码
Python抓包并解析json爬虫 在使用Python爬虫的时候,通过抓包url,打开url可能会遇见以下类似网址,打开后会出现类似这样的界面,无法继续进行爬虫: 例如: 需要爬取网页中第二页的数据时 ...
- c语言json结构体_C语言解析JSON源码
2020-01-09 关键字:cJSON.linux JSON解析 JSON 是一种在互联网领域内很常用的轻量级数据交换协议. 它与 XML 的地位差不多,但就笔者而言,笔者更喜欢 JSON 的风格, ...
- json解析函数c语言,C语言解析json类型数据
转自:http://buluzhai.iteye.com/blog/845404 首先感谢作者!! 先看json的数据结构 c中没有对象,所以json数据是采用链表存储的 C代码如下: typed ...
- Go语言解析Json(使用jsonparser)
原文地址:https://blog.csdn.net/benben_2015/article/details/81119804 jsonparser github地址:https://github. ...
- c语言解析json数据
http://buluzhai.iteye.com/blog/845404 我使用的是cJSON:http://sourceforge.net/projects/cjson/ 先看json的数据结构 ...
- 在C语言中解析json配置文件
业务需求 在C或者C++项目中常常需要解析配置文件,我们常见的配置文件格式一般就是.ini,xml,lua或者是一般的text文件,这些格式比较恼人的一个问题就是数据格式过于冗余,或者功能不够强大,不 ...
最新文章
- Java Json API:Gson使用简单入门
- 全国计算机等级考试二级vb上机模拟软件,(全国计算机等级考试二级Vb上机模拟9-13.doc...
- 在.Net程序中使用log4net记录日志(示例)
- 简述PHP中有哪些运算符,PHP运算符简述
- 找不到媳妇给愁的。。。
- 2021-07-12
- JenKins使用pm2部署.net core网站
- SIGIR 2019 | 基于人类阅读行为模式的机器阅读理解
- 20-forEach循环语句
- ddos攻击工具_简单有效的ddos攻击防御方法
- python查找文件夹下所有文件_如何用Python实现查找/目录下的文件夹或文件,致谢...
- DsoFramer-辛酸泪史
- 关于html转换为pdf案例的一些测试与思考
- 审计机构不用计算机审计,计算机审计存在哪些风险
- Vue3+TypeScript
- Linux内核中的IPSEC实现(1)
- 【STM32利用CuBe MX生成HID设备】1-熟悉软件以及生成一个8键的游戏控制器
- AsyncTask如何使用
- Redis缓存有效时间设置到第二天凌晨的秒数
- SuperMap iClient for JavaScript常见问题解答集锦(十二)