cJSON介绍
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,易于人阅读和编写,同时也易于机器解析和生成。有关JSON的知识,请参考《介绍 JSON》。
cJSON是一个用C语言实现的Json处理程序,代码量不大,只有一个头文件cJSON.h和一个源文件cJSON.c,用起来比较方便。从sourceforge上可以下载。
在cJSON.h中定义了JSON的数据结构,如下:
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;
定义还是蛮清楚的,用链表存储方式存储JSON,type代表这个结构体的类型,包括数、字符串、数组、json对象等。如果代表的是实数,则valuedouble就存储了这个实数取值,如果是int型的,则取valueint,其他类似。 type的值在cJSON.h中定义了相应的宏。 / 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.h中的函数定义也比较明白,看函数名和注释应该能明白函数的功能,下面列举了几个常用的函数:
extern cJSON *cJSON_Parse(const char *value); /* Supply a block of JSON, and this returns a cJSON object you can interrogate. Call cJSON_Delete when finished. */
extern char *cJSON_Print(cJSON *item); /* Render a cJSON entity to text for transfer/storage. Free the char* when finished. */
extern char *cJSON_PrintUnformatted(cJSON *item); /* Render a cJSON entity to text for transfer/storage without any formatting. Free the char* when finished. */
extern void cJSON_Delete(cJSON *c); /* Delete a cJSON entity and all subentities. */
/*下面的这些函数主要用来创建json*/
extern cJSON *cJSON_CreateObject(void);
/* Macros for creating things quickly. */
#define cJSON_AddNullToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateNull())
#define cJSON_AddTrueToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateTrue())
#define cJSON_AddFalseToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateFalse())
#define cJSON_AddBoolToObject(object,name,b) cJSON_AddItemToObject(object, name, cJSON_CreateBool(b))
#define cJSON_AddNumberToObject(object,name,n) cJSON_AddItemToObject(object, name, cJSON_CreateNumber(n))
#define cJSON_AddStringToObject(object,name,s) cJSON_AddItemToObject(object, name, cJSON_CreateString(s))
下面是写了个测试程序,输出是
{
"student": {
"name": "Jack",
"sex": "male",
"age": 20,
"score": {
"Chinese": 90,
"English": 85.500000
}
}
}
程序代码很简单,按照自己需要的json格式来添加相应的内容即可。
#include <stdio.h>
#include <stdlib.h>
#include "cJSON.h"
cJSON * create_json()
{
cJSON * json = 0;
cJSON * json_student = 0;
cJSON * json_score = 0;
//创建json的根节点
json = cJSON_CreateObject();
//创建json子节点json_student
json_student = cJSON_CreateObject();
cJSON_AddStringToObject(json_student,"name","Jack"); //为json_student添加name属性,值为Jack
cJSON_AddStringToObject(json_student,"sex","male"); //为json_student添加sex属性,值为male
cJSON_AddNumberToObject(json_student,"age",20); //为json_student添加age属性,值为20
//创建json_student的子节点json_score
json_score = cJSON_CreateObject();
cJSON_AddNumberToObject(json_score,"Chinese",90.0); //为json_score添加Chinese属性,值为90
cJSON_AddNumberToObject(json_score,"English",85.5); //为json_score添加English属性,值为85.5
cJSON_AddItemToObject(json_student,"score",json_score); //json_student添加子节点json_score,键为score
cJSON_AddItemToObject(json,"student",json_student); //json添加子节点json_student,键为student
return json;
}
int main(int argc,char ** argv)
{
//创建json
cJSON * json = create_json();
//打印json
char * str_json = cJSON_Print(json);
printf("%s\n",str_json);
//释放json
cJSON_Delete(json);
free(str_json);
return 0;
}