超轻量级Cjson
实践记录:int main() 记录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; 1、cJSON存储的时候是采用链表存储的,其访问方式很像一颗树。每一个节点可以有兄妹节点,通过next/prev指针来查找,它类似双向链表;每个节点也可以有孩子节点,通过child指针来访问,进入下一层。 不过,只有节点是对象或数组才可以有孩子节点。 2、type一共有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类型,则valueint或valuedouble中存储着值,若你期望的是int,则访问valueint,若期望的是double,则访问valuedouble,可以得到值。 若是String类型的,则valuestring中存储着值,可以访问valuestring得到值。 3、string中存放的是这个节点的名字。 用法: 1、只需在函数中includecJSON.h头文件,然后和cJSON.c或库文件libcJSON.a一起编译即可使用。 2、具体函数用法详见cJSON.h中注释 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- http://apps.hi.baidu.com/share/detail/42828576 参考资料:
|
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的话强烈推荐~
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!