一 Cjson库的使用
什么是json在这里就不再赘述了,直入主题,使用Cjson库需要cJSON.h和cJSON.c。下载链接https://download.csdn.net/download/weixin_42765818/12304807
1.我们第一步先了解cJSON的数据结构
#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
#define cJSON_IsReference 256
#define cJSON_StringIsConst 512
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, if this item is the child of, or is in the list of subitems of an object. */
} cJSON;
通过前面的next,prev很容易看出这个结构是链表,child指向孩子节点,type表示value的类型(json就是key,value键值对形式的),其余的成员我们看注释就应该大致知道如何使用了,主要做类型判断用的。json下的value可以是什么类型,通过这个宏也可以看的很清楚了。 | |
---|---|
2.我们介绍一下cJSON中的库函数,着重介绍一些常用的函数
2.1解析json ,主要使用以下函数
extern cJSON *cJSON_Parse(const char *value);
这个函数很简单,我们看出参和入参大概就能猜出来了,进去的是个char*,出来一个cJSON*。这就是个将json格式的字符串,变为json格式。
例如:char *data = "{\"id\": \"9527\",\"name\": \"小张\"}";
cJSON *cjson_data = cJSON_Parse(data);
!!注意这个函数会在堆上开辟内存,使用完毕后记得调用cJSON_Delete(cJSON *c),切记不要用free释放cJSON类型的指针,用人家自己的安全一点;
extern char *cJSON_Print(cJSON *item);
这个也很简单,和cJSON_Parse反着来,将cJSON转为char*,也要记得释放内存,这个函数是使用free(char *c);
extern char *cJSON_PrintUnformatted(cJSON *item);
这个函数是从cjson对象中获取无格式的json对象,使用较少。
extern char *cJSON_PrintBuffered(cJSON *item,int prebuffer,int fmt);
这个我也没用过,但他使用malloc了,也要注意释放内存;
extern void cJSON_Delete(cJSON *c);
这个不多说了
extern int cJSON_GetArraySize(cJSON *array);
获取json_array有多少个元素
extern cJSON *cJSON_GetArrayItem(cJSON *array,int item);
获取json_array的元素,和数组通过索引获取元素一样的
extern cJSON *cJSON_GetObjectItem(cJSON *object,const char *string);
通过key获取value
extern const char *cJSON_GetErrorPtr(void);
获取错误字符串
2.2构造json,主要使用以下函数
extern cJSON *cJSON_CreateNull(void);
extern cJSON *cJSON_CreateTrue(void);
extern cJSON *cJSON_CreateFalse(void);
extern cJSON *cJSON_CreateBool(int b);
extern cJSON *cJSON_CreateNumber(double num);
extern cJSON *cJSON_CreateString(const char *string);
extern cJSON *cJSON_CreateArray(void);
extern cJSON *cJSON_CreateObject(void);
/* These utilities create an Array of count items. */
extern cJSON *cJSON_CreateIntArray(const int *numbers,int count);
extern cJSON *cJSON_CreateFloatArray(const float *numbers,int count);
extern cJSON *cJSON_CreateDoubleArray(const double *numbers,int count);
extern cJSON *cJSON_CreateStringArray(const char **strings,int count);
这些库函数,光看函数名称就清楚什么意思了,create开头的都是构造json。顺带一提几乎所有链表结构,在释放内存时只需要释放头就可以了,cJSON也是这样。
2.3给json添加元素,使用以下函数
/* Append item to the specified array/object. */
extern void cJSON_AddItemToArray(cJSON *array, cJSON *item);
extern void cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item);
extern void cJSON_AddItemToObjectCS(cJSON *object,const char *string,cJSON *item); /* Use this when string is definitely const (i.e. a literal, or as good as), and will definitely survive the cJSON object */
/* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */
extern void cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item);
extern void cJSON_AddItemReferenceToObject(cJSON *object,const char *string,cJSON *item);
举个例子:
cJSON *food = cJSON_CreateObject();
cJSON *fruit = cJSON_CreateArray();
cJSON *meat = cJSON_CreateArray();
cJSON *cjson_apple = cJSON_CreateObject();
cJSON *cjson_banana = cJSON_CreateObject();
cJSON_AddItemToObject(cjson_apple, "苹果", cJSON_CreateString("apple"));
cJSON_AddItemToObject(cjson_banana, "香蕉", cJSON_CreateString("banana"));
cJSON_AddItemToArray(fruit, cjson_apple);
cJSON_AddItemToArray(fruit, cjson_banana);
cJSON *cjson_chicken = cJSON_CreateObject();
cJSON *cjson_fish = cJSON_CreateObject();
cJSON_AddItemToObject(cjson_chicken, "鸡肉", cJSON_CreateString("chicken"));
cJSON_AddItemToObject(cjson_fish, "鱼肉", cJSON_CreateString("fish"));
cJSON_AddItemToArray(meat, cjson_chicken);
cJSON_AddItemToArray(meat, cjson_fish);
cJSON_AddItemToObject(food,"水果",fruit);
cJSON_AddItemToObject(food,"肉",meat);
char *p=cJSON_Print(food);
printf(" *******%s\n",cJSON_Print(food));
free(p);
cJSON_Delete(food);
/*打印结果 *******{
"水果": [{
"苹果": "apple"
}, {
"香蕉": "banana"
}],
"肉": [{
"鸡肉": "chicken"
}, {
"鱼肉": "fish"
}]
}*/
小结:cJSON非常简单,注意使用完cJSON_Print后free,使用完cJSON_Parse后cJSON_Delete,oreate对象后记得释放json对象。解析json时要仔细一点,平时写代码的时候一定要先想好再写,不要小瞧简单的东西,你要是不仔细不认真,会浪费很多时间,下篇介绍一下Qjson。