加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

超轻量级Cjson

发布时间:2020-12-16 19:25:34 所属栏目:百科 来源:网络整理
导读:实践记录: int main() { char *out ; cJSON *root,*fmt; root=cJSON_CreateObject(); cJSON_AddItemToObject(root,"name",cJSON_CreateString("Jack ("Bee") Nimble")); cJSON_AddItemToObject(root,"format",fmt=cJSON_CreateObject()); cJSON_AddString

实践记录:

int main()
{
char *out ;
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("%sn",out);
//free(out);
out=cJSON_Print(fmt);
printf("%sn",out);
free(out);
return 1;
}

记录2


数组:

#include <cJSON.h>
int main()
{
	char *out ;
	cJSON *root,*watcher,*tmp;
	root=cJSON_CreateObject();
	cJSON_AddItemToObject(root,"ID",cJSON_CreateString("LiaoNingHao"));
	cJSON_AddItemToObject(root,"WarningLevel",cJSON_CreateString("1"));
	cJSON_AddItemToObject(root,"Lon",cJSON_CreateString("113.123"));
	cJSON_AddItemToObject(root,"Lat",cJSON_CreateString("21.123"));
	cJSON_AddItemToObject(root,"Tips",cJSON_CreateString(""));

	cJSON_AddItemToObject(root,"Watchers",watcher=cJSON_CreateArray());

	tmp=cJSON_CreateObject();
	cJSON_AddItemToObject(tmp,"WatcherName",cJSON_CreateString("content2"));
	cJSON_AddItemToObject(tmp,"WatcherRtspAddr",cJSON_CreateString("rtsp://admin:12345@192.168.20.76/h264/ch1/main/av_stream"));
	cJSON_AddItemToArray(watcher,tmp);

	////cJSON_Delete(tmp);
	tmp=cJSON_CreateObject();
	cJSON_AddItemToObject(tmp,cJSON_CreateString("content1"));
	cJSON_AddItemToObject(tmp,cJSON_CreateString("rtsp://admin:12345@192.168.30.194/h264/ch1/main/av_stream"));
	cJSON_AddItemToArray(watcher,tmp);

	
	out=cJSON_Print(root);
	printf("%sn",out);

	free(out);
	cJSON_Delete(root);

	getchar();
	return 1;
}

解析:

	cJSON *root = cJSON_Parse(jsonSrc_);
	cJSON *area = cJSON_GetObjectItem(root,"DisableAreas");

	int areaNum = cJSON_GetArraySize(area) ;
	for(int i=0 ;i<areaNum;i++){
		printf("x:%dn",cJSON_GetObjectItem(cJSON_GetArrayItem(area,i),"x")->valueint);
		printf("y:%dn","y")->valueint);
		printf("width:%dn","width")->valueint);
		printf("height:%dn","height")->valueint);
	}
	cJSON_Delete(root);




CJSON用法:

cJSON简介:

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

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

cJSON结构体:

typedefstruct cJSON {

structcJSON *next,*prev;

struct cJSON *child;

int type;

char * valuestring;

int valueint;

double valuedouble;

char *string;

}cJSON;

1cJSON存储的时候是采用链表存储的,其访问方式很像一颗树。每一个节点可以有兄妹节点,通过next/prev指针来查找,它类似双向链表;每个节点也可以有孩子节点,通过child指针来访问,进入下一层。

不过,只有节点是对象或数组才可以有孩子节点。

2type一共有7种取值,分别是:

#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

若是Number类型,则valueintvaluedouble中存储着值,若你期望的是int,则访问valueint,若期望的是double,则访问valuedouble,可以得到值。

若是String类型的,则valuestring中存储着值,可以访问valuestring得到值。

3string中存放的是这个节点的名字。

用法:

1、只需在函数中includecJSON.h头文件,然后和cJSON.c或库文件libcJSON.a一起编译即可使用。

2、具体函数用法详见cJSON.h中注释

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

http://apps.hi.baidu.com/share/detail/42828576

c语言解析json数据

c语言解析json数据
文章分类:C++编程
我使用的是cJSON:http://sourceforge.net/projects/cjson/

先看json的数据结构
c中没有对象,所以json数据是采用链表存储的
C代码
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;

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,109); line-height:26px"> 比如你有一个json数据
Javascript代码
{
"name": "Jack ("Bee") Nimble",
"format": {
"type": "rect",
"width": 1920,
"height": 1080,
"interlace": false,
"frame rate": 24
}
}

{
"name": "Jack ("Bee") Nimble",109); line-height:26px"> 那么你可以

1:将字符串解析成json结构体。

C代码
cJSON *root = cJSON_Parse(my_json_string);

cJSON *root = cJSON_Parse(my_json_string);

2:获取某个元素

C代码
cJSON *format = cJSON_GetObjectItem(root,"format");
int framerate = cJSON_GetObjectItem(format,"frame rate")->valueint;

cJSON *format = cJSON_GetObjectItem(root,"frame rate")->valueint;

3:讲json结构体转换成字符串

C代码
char *rendered=cJSON_Print(root);

char *rendered=cJSON_Print(root);

4:删除

C代码
cJSON_Delete(root);

cJSON_Delete(root);

5:构建一个json结构体

C代码
cJSON *root,*fmt;
root=cJSON_CreateObject();
cJSON_AddItemToObject(root,cJSON_CreateString("Jack ("Bee") Nimble"));
cJSON_AddItemToObject(root,fmt=cJSON_CreateObject());
cJSON_AddStringToObject(fmt,"rect");
cJSON_AddNumberToObject(fmt,1920);
cJSON_AddNumberToObject(fmt,1080);
cJSON_AddFalseToObject (fmt,"interlace");
cJSON_AddNumberToObject(fmt,24);


参考资料:

JSON是网络上常见的数据传输格式之一,尤其AJAX常用。最近想用C++解析JSON,查了一下JSON的官方网站,翻出来一个不错的库——cJSON库。貌似使用的人不是很多,但是只有两个文件,代码量不大,基本实现了常见的所有功能,用起来还是挺方便的。
cJSON的网址:http://sourceforge.net/projects/cjson/
打开cJSON.h文件看看数据类型和函数名基本上就能上手了。
主要数据类型:

1
2
3
4
5
6
7
8
9
10
11
12
typedef struct cJSON {
	struct cJSON *next,*prev;	/* next/prev allow you to walk array/object chains. Alternatively,use GetArraySize/GetArrayItem/GetObjectItem */
	struct cJSON *child;		/* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */

	int type;					/* The type of the item,as above. */

	char *valuestring;			/* The item's string,if type==cJSON_String */
	int valueint;				/* The item's number,if type==cJSON_Number */
	double valuedouble;			/* The item's number,if type==cJSON_Number */

	char *string;				/* The item's name string,or is in the list of subitems of an object. */
} cJSON;

他定义了一个cJSON结构体,使用了链表存储方式。type代表这个结构体的类型,包括数、字符串、数组、json对象等。如果代表的是实数,则valuedouble就存储了这个实数取值,其余成员类似容易看懂。
几个常用函数

1
2
3
4
5
6
7
8
9
extern cJSON *cJSON_Parse(const char *value);//解析一个json字符串为cJSON对象
extern char  *cJSON_Print(cJSON *item);//将json对象转换成容易让人看清结构的字符串
extern char  *cJSON_PrintUnformatted(cJSON *item);//将json对象转换成一个很短的字符串,无回车
extern void   cJSON_Delete(cJSON *c);//删除json对象
extern int	  cJSON_GetArraySize(cJSON *array);//返回json数组大小
extern cJSON *cJSON_GetArrayItem(cJSON *array,int item);//返回json数组中指定位置对象
extern cJSON *cJSON_GetObjectItem(cJSON *object,const char *string);//返回指定字符串对应的json对象
extern cJSON *cJSON_CreateIntArray(int *numbers,int count);//生成整型数组json对象
extern void cJSON_AddItemToArray(cJSON *array,cJSON *item);//向数组中添加元素

README文件中也提供了一些简单的说明和例子程序。cJSON是个简单而好用的C语言JSON库,需要用C/C++处理JSON的话强烈推荐~

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读