目录
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,易于阅读和编写,同时也易于机器解析和生成,它是JavaScript编程语言的一个子集,但采用完全独立于语言的文本格式,C、Python、C++、Java、PHP、Go等编程语言都支持 JSON。
cJSON库函数下载地址
git clone https://github.com/DaveGamble/cJSON
1.cJSON结构体
在下载的cJSON库中,cJSON.h中定义了一个非常重要的cJSON结构体,cJSON用cJSON结构体来表示一个json数据,cJSON结构体结构体如下:
typedef struct cJSON
{
/* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
struct cJSON *next;
struct cJSON *prev;
/* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
struct cJSON *child;
/* The type of the item, as above. */
int type;
/* The item's string, if type==cJSON_String and type == cJSON_Raw */
char *valuestring;
/* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */
int valueint;
/* The item's number, if type==cJSON_Number */
double valuedouble;
/* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
char *string;
} cJSON;
cJSON的设计很巧妙,它不是将一整段JSON数据抽象出来,而是将其中的一条JSON数据抽象出来,也就是一个键值对,用上面的结构体来表示,其中用来存放值的成员列表如下:
cJSON数据库是一个双向链表,next指向下一个节点、prev指向上一个节点,child指向子节点。
type:节点类型值,有如下七种类型:
#define cJSON_False 0
#define cJSON_Ture 1
#define cJSON_NULL 2
#define cJSON_Number 3
#define cJSON_Srting 4
#define cJSON_Array 5
#define cJSON_Obeject 6
valuestring:如果该键值(type)类型是字符串,则该指针指向键值
valueint:如果该键值(type)类型是整数,则该指针指向键值
valuedouble:如果该键值(type)类型是浮点数,则该指针指向键值
2.cJSON常用API
(1)创建和释放JSON对象或数组
cJSON *cJSON_CreateObject();
//创建一个json对象,返回一个cJSON结构体类型的指针。
cJSON *cJSON_CreateArray();
//创建一个数组对象,返回一个cJSON结构体类型的指针。
cJSON *cJSON_CreateString(const char *string);
//创建一个字符串对象,传入一个char *类型的字符串,返回一个cJSON结构体类型的指针。
void cJSON_Delete(cJSON *item);
//释放json对象或数组
(2)在JSON对象中添加键值对
void cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item);
//向json对象中添加一对元素,object为json对象,string为加入一对元素中的name,item为加入一对元素中的value。
(3)在JSON数组中添加元素
void cJSON_AddItemToArray(cJSON *array, cJSON *item);
//;=将一个元素添加到JSON数组中。
(4) 获取JSON对象中的值
cJSON *cJSON_GetObjectItem(const cJSON *object, const char *string);
//获取JSON对象中指定键的值。
(5)获取JSON数组中的元素
cJSON *cJSON_GetArrayItem(const cJSON *array, int index);
//获取JSON数组中指定索引的元素。
(6)解析JSON字符串和生成JSON字符串
cJSON *cJSON_Parse(const char *value);
//将JSON字符串解析为JSON对象。
char *cJSON_Print(const cJSON *item);
//将JSON对象转换为格式化的JSON字符串。
3.测试代码
#include <stdio.h>
#include <stdlib.h>
#include "cJSON.h"
int main() {
// 创建JSON对象和数组
cJSON *root = cJSON_CreateObject();
cJSON *array = cJSON_CreateArray();
// 向JSON对象中添加键值对
cJSON_AddItemToObject(root, "name", cJSON_CreateString("John"));
cJSON_AddItemToObject(root, "age", cJSON_CreateNumber(25));
// 向JSON数组中添加元素
cJSON_AddItemToArray(array, cJSON_CreateString("Apple"));
cJSON_AddItemToArray(array, cJSON_CreateString("Banana"));
cJSON_AddItemToArray(array, cJSON_CreateString("Orange"));
// 将JSON数组添加到JSON对象中
cJSON_AddItemToObject(root, "fruits", array);
// 将JSON对象转换为字符串并打印
char *jsonStr = cJSON_Print(root);
printf("Generated JSON string:\n%s\n", jsonStr);
// 释放JSON对象和字符串
cJSON_Delete(root);
free(jsonStr);
// 解析JSON字符串
const char *json = "{\"name\":\"Alice\",\"age\":30,\"fruits\":[\"Mango\",\"Grapes\"]}";
cJSON *parsedJson = cJSON_Parse(json);
// 从JSON对象中获取键值对的值
cJSON *nameItem = cJSON_GetObjectItem(parsedJson, "name");
cJSON *ageItem = cJSON_GetObjectItem(parsedJson, "age");
if (nameItem && nameItem->valuestring && ageItem && ageItem->valueint) {
printf("\nParsed JSON:\n");
printf("Name: %s\n", nameItem->valuestring);
printf("Age: %d\n", ageItem->valueint);
// 从JSON数组中获取元素
cJSON *fruitsArray = cJSON_GetObjectItem(parsedJson, "fruits");
if (fruitsArray && cJSON_IsArray(fruitsArray)) {
printf("Fruits: ");
cJSON *fruitItem;
cJSON_ArrayForEach(fruitItem, fruitsArray) {
if (fruitItem && fruitItem->valuestring) {
printf("%s ", fruitItem->valuestring);
}
}
printf("\n");
}
}
// 释放解析后的JSON对象
cJSON_Delete(parsedJson);
return 0;
}