cJSON库介绍及编程实例

JSON是一种轻量级的数据交换格式,基于JavaScript的一个子集。JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯。这些特性使JSON成为理想的数据交换语言。易于人阅读与编写,同时也易于机器解析和生成。

cJSON是一个超轻巧,携带方便,单文件,简单的可以作为ANSI-C标准的JSON解析器。

cJSON下载地址:点击打开链接

查看cJSON代码可以看到,cJSON结构体如下:

typedef struct cJSON {
	struct cJSON *next,*prev;
	struct cJSON *child;

	int type;

	char *valuestring;
	int valueint;
	double valuedouble;

	char *string;
} cJSON;

cJSON以双链表的形式存储。

共有7种类型:

/* cJSON Types: */
#define cJSON_False 0
#define cJSON_True 1
#define cJSON_NULL 2
#define cJSON_Number 3
#define cJSON_String 4
#define cJSON_Array 5
#define cJSON_Object 6

cJSON实例:

#include<stdio.h>
#include<stdlib.h>
#include"cJSON.h"

int main(int argc,const char *argv[])
{
	cJSON *root,*fmt,*json,*format;
	char *out;
	int i;
	//创建JSON字符串
	root = cJSON_CreateObject();
	cJSON_AddItemToObject(root,"name",cJSON_CreateString("Jack"));
	cJSON_AddItemToObject(root,"format",fmt = cJSON_CreateObject());
	cJSON_AddStringToObject(fmt,"type","rect");
	cJSON_AddNumberToObject(fmt,"width",1000);
	cJSON_AddNumberToObject(fmt,"height",2000.123);
//	cJSON_AddFalseToObject(fmt,"interlace");
	cJSON_AddNumberToObject(fmt,"frame rate",24);
	out = cJSON_Print(root);	//out中保存的是JSON格式的字符串
	cJSON_Delete(root);		//删除cJSON
	printf("%s\n",out);		//终端打印输出

	json = cJSON_Parse(out);	//解析JSON格式的字符串
	free(out);			//释放字符串空间
	if(!json)
	{
		printf("Error before:[%s]\n",cJSON_GetErrorPtr());
	}
	else
	{
		//提取出各个数据并打印输出
		char *name = cJSON_GetObjectItem(json,"name")->valuestring;
		printf("name: %s\n",name);
		format = cJSON_GetObjectItem(json,"format");
		char *type = cJSON_GetObjectItem(format,"type")->valuestring;
		printf("type: %s\n",type);
		int width = cJSON_GetObjectItem(format,"width")->valueint;			
		printf("width: %d\n",width);
		double height = cJSON_GetObjectItem(format,"height")->valuedouble;		
		printf("width: %f\n",height);
		int framerate = cJSON_GetObjectItem(format,"frame rate")->valueint;

		printf("framerate: %d\n",framerate);
		
	}
}

编译:把cJSON.c和cJSON.h放在编写代码目录下,同时编译cJSON和工作代码:gcc  cJSON.c  test.c  -o  test  -lm

运行结果:




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
cJSON是一个轻量级的JSON解析器和生成器,适用于C语言。它提供了一些方便的API用于解析和生成JSON数据。下面是cJSON的常用函数介绍: ### 1. cJSON_Parse 解析JSON字符串,返回cJSON对象。 ```c cJSON *cJSON_Parse (const char *value); ``` ### 2. cJSON_Print 将cJSON对象转换成字符串。 ```c char *cJSON_Print (const cJSON *item); ``` ### 3. cJSON_AddItemToObject 将一个cJSON对象作为子对象添加到另一个cJSON对象中。 ```c void cJSON_AddItemToObject (cJSON *object, const char *string, cJSON *item); ``` ### 4. cJSON_GetObjectItem 获取一个cJSON对象中指定名称的子对象。 ```c cJSON *cJSON_GetObjectItem (const cJSON *object, const char *string); ``` ### 5. cJSON_CreateObject 创建一个空的cJSON对象。 ```c cJSON *cJSON_CreateObject (void); ``` ### 6. cJSON_CreateString 创建一个cJSON字符串对象。 ```c cJSON *cJSON_CreateString (const char *string); ``` ### 7. cJSON_CreateNumber 创建一个cJSON数字对象。 ```c cJSON *cJSON_CreateNumber (double num); ``` ### 8. cJSON_CreateBool 创建一个cJSON布尔对象。 ```c cJSON *cJSON_CreateBool (int b); ``` ### 9. cJSON_CreateArray 创建一个空的cJSON数组对象。 ```c cJSON *cJSON_CreateArray (void); ``` ### 10. cJSON_Delete 释放一个cJSON对象及其内存。 ```c void cJSON_Delete (cJSON *item); ``` 上述函数是cJSON中的常用函数,使用这些函数可以实现JSON数据的解析和生成。需要注意的是,在使用完cJSON对象后需要调用cJSON_Delete函数释放对象及其内存。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值