文章目录
引言(Introduction)
写在文章开头,非淡泊不以明志,非宁静无以致远。
在了解了JSON数据类型之后,可以开始使用cJSON进行处理json数据类型了。本文主要讲解cJSON中的创建对象,添加数据,删除数据,查找数据以及更改数据四种应用场景。
编译(Compile)
使用cJSON需要在文件中包含"cJSON.h",编译的命令为:
gcc cJSON.c **.c -o **
1. 创建对象(Create object)
创建一个json对象只需要调用函数"cJSON_CreateObject"即可,其会调用"cJSON.c"中的"cJSON_New_Item"函数,使用钩子分配内存。(后续会进行源码分析,所以后面例子不再深入介绍)
#include <stdio.h>
#include <stdlib.h>
#include "cJSON.h"
int CJSON_CDECL main(void){
cJSON *root = NULL;
root = cJSON_CreateObject(); // 创建对象
cJSON_Delete(root);
return 0;
}
在函数结束的时候要记得释放内存。
2. 添加数据(Add Data)
本实验构造的json结构如下:
{
"name": "Zhang San", // string
"sex": 1, // boolen
"height": 1.8 // number
"family": [ // array
{
"name": "Zhang Si",
"relationship": "Father"
},
{
"name": "Li Si",
"relationship": "Mother"
}
],
"birthday": { // object
"year": 2000,
"month": 1,
"day":1
},
}
需要明白cJSON是json结构的一个元素,可以看作是链表的一个结点。这一点是非常重要的。以上json的数据结构如下图所示。
其代码实现如下:
#include <stdio.h>
#include <stdlib.h>
#include "cJSON.h"
int CJSON_CDECL main(void){
cJSON *root = NULL;
cJSON *family = NULL;
cJSON *father = NULL;
cJSON *mother = NULL;
cJSON *birthday = NULL;
char *output = NULL;
root = cJSON_CreateObject(); // 创建Object对象
cJSON_AddItemToObject(root, "name", cJSON_CreateString("Zhang San")); // 添加String对象
cJSON_AddTrueToObject(root, "sex"); // 添加Boolen对象
cJSON_AddNumberToObject(root, "height", 1.8); // 添加Number对象
family = cJSON_AddArrayToObject(root, "family"); // 添加Array对象
// 往Array中添加数据
cJSON_AddItemToArray(family, father = cJSON_CreateObject()); // 添加Object对象
cJSON_AddItemToObject(father, "name", cJSON_CreateString("Zhang Si")); // 添加String对象
cJSON_AddItemToObject(father, "relationship", cJSON_CreateString("Father")); // 添加String对象
cJSON_AddItemToArray(family, mother = cJSON_CreateObject()); // 添加Object对象
cJSON_AddItemToObject(mother, "name", cJSON_CreateString("Li Si")); // 添加String对象
cJSON_AddItemToObject(mother, "relationship", cJSON_CreateString("Mother")); // 添加String对象
cJSON_AddItemToObject(root, "birthday", birthday = cJSON_CreateObject()); // 添加Object对象
cJSON_AddNumberToObject(birthday, "year", 2000); // 添加Number对象
cJSON_AddNumberToObject(birthday, "month", 1); // 添加Number对象
cJSON_AddNumberToObject(birthday, "day", 1); // 添加Number对象
output = cJSON_Print(root);
printf("%s\n", output);
// 释放内存不能少
free(output);
cJSON_Delete(root);
return 0;
}
3. 删除数据(Delete Data)
删除数据可以从Array中删除,也可以从Object中删除。下面将分别删除"father"与"birthday"。
#include <stdio.h>
#include <stdlib.h>
#include "cJSON.h"
int CJSON_CDECL main(void){
cJSON *root = NULL;
cJSON *family = NULL;
cJSON *father = NULL;
cJSON *mother = NULL;
cJSON *birthday = NULL;
char *output = NULL;
root = cJSON_CreateObject(); // 创建Object对象
cJSON_AddItemToObject(root, "name", cJSON_CreateString("Zhang San")); // 添加String对象
cJSON_AddTrueToObject(root, "sex"); // 添加Boolen对象
cJSON_AddNumberToObject(root, "height", 1.8); // 添加Number对象
family = cJSON_AddArrayToObject(root, "family"); // 添加Array对象
// 往Array中添加数据
cJSON_AddItemToArray(family, father = cJSON_CreateObject()); // 添加Object对象
cJSON_AddItemToObject(father, "name", cJSON_CreateString("Zhang Si")); // 添加String对象
cJSON_AddItemToObject(father, "relationship", cJSON_CreateString("Father")); // 添加String对象
cJSON_AddItemToArray(family, mother = cJSON_CreateObject()); // 添加Object对象
cJSON_AddItemToObject(mother, "name", cJSON_CreateString("Li Si")); // 添加String对象
cJSON_AddItemToObject(mother, "relationship", cJSON_CreateString("Mother")); // 添加String对象
cJSON_AddItemToObject(root, "birthday", birthday = cJSON_CreateObject()); // 添加Object对象
cJSON_AddNumberToObject(birthday, "year", 2000); // 添加Number对象
cJSON_AddNumberToObject(birthday, "month", 1); // 添加Number对象
cJSON_AddNumberToObject(birthday, "day", 1); // 添加Number对象
output = cJSON_Print(root);
printf("%s\n", output);
cJSON_DeleteItemFromArray(family, 0); // 从Array中删除一个Item/Object
output = cJSON_Print(root);
printf("%s\n", output);
cJSON_DeleteItemFromObject(root, "birthday"); // 从Object中删除一个Item
output = cJSON_Print(root);
printf("%s\n", output);
// 释放内存不能少
free(output);
cJSON_Delete(root);
return 0;
}
4. 查找数据(Search Data)
查找数据主要是实现一级查找,即从当前中括号的一级进行查找,不会进行递归查找。
#include <stdio.h>
#include <stdlib.h>
#include "cJSON.h"
int CJSON_CDECL main(void){
cJSON *root = NULL;
cJSON *family = NULL;
cJSON *father = NULL;
cJSON *mother = NULL;
cJSON *birthday = NULL;
cJSON *search = NULL;
char *output = NULL;
root = cJSON_CreateObject(); // 创建Object对象
cJSON_AddItemToObject(root, "name", cJSON_CreateString("Zhang San")); // 添加String对象
cJSON_AddTrueToObject(root, "sex"); // 添加Boolen对象
cJSON_AddNumberToObject(root, "height", 1.8); // 添加Number对象
family = cJSON_AddArrayToObject(root, "family"); // 添加Array对象
// 往Array中添加数据
cJSON_AddItemToArray(family, father = cJSON_CreateObject()); // 添加Object对象
cJSON_AddItemToObject(father, "name", cJSON_CreateString("Zhang Si")); // 添加String对象
cJSON_AddItemToObject(father, "relationship", cJSON_CreateString("Father")); // 添加String对象
cJSON_AddItemToArray(family, mother = cJSON_CreateObject()); // 添加Object对象
cJSON_AddItemToObject(mother, "name", cJSON_CreateString("Li Si")); // 添加String对象
cJSON_AddItemToObject(mother, "relationship", cJSON_CreateString("Mother")); // 添加String对象
cJSON_AddItemToObject(root, "birthday", birthday = cJSON_CreateObject()); // 添加Object对象
cJSON_AddNumberToObject(birthday, "year", 2000); // 添加Number对象
cJSON_AddNumberToObject(birthday, "month", 1); // 添加Number对象
cJSON_AddNumberToObject(birthday, "day", 1); // 添加Number对象
output = cJSON_Print(root);
printf("%s\n", output);
search = cJSON_GetObjectItem(root, "birthday"); // 查找"birthday",返回cJSON*对象
output = cJSON_Print(search);
printf("%s\n", output);
// 释放内存不能少
free(output);
cJSON_Delete(root);
return 0;
}
5. 更改数据(Update Data)
#include <stdio.h>
#include <stdlib.h>
#include "cJSON.h"
int CJSON_CDECL main(void){
cJSON *root = NULL;
cJSON *family = NULL;
cJSON *father = NULL;
cJSON *mother = NULL;
cJSON *birthday = NULL;
cJSON *search = NULL;
char *output = NULL;
root = cJSON_CreateObject(); // 创建Object对象
cJSON_AddItemToObject(root, "name", cJSON_CreateString("Zhang San")); // 添加String对象
cJSON_AddTrueToObject(root, "sex"); // 添加Boolen对象
cJSON_AddNumberToObject(root, "height", 1.8); // 添加Number对象
family = cJSON_AddArrayToObject(root, "family"); // 添加Array对象
// 往Array中添加数据
cJSON_AddItemToArray(family, father = cJSON_CreateObject()); // 添加Object对象
cJSON_AddItemToObject(father, "name", cJSON_CreateString("Zhang Si")); // 添加String对象
cJSON_AddItemToObject(father, "relationship", cJSON_CreateString("Father")); // 添加String对象
cJSON_AddItemToArray(family, mother = cJSON_CreateObject()); // 添加Object对象
cJSON_AddItemToObject(mother, "name", cJSON_CreateString("Li Si")); // 添加String对象
cJSON_AddItemToObject(mother, "relationship", cJSON_CreateString("Mother")); // 添加String对象
cJSON_AddItemToObject(root, "birthday", birthday = cJSON_CreateObject()); // 添加Object对象
cJSON_AddNumberToObject(birthday, "year", 2000); // 添加Number对象
cJSON_AddNumberToObject(birthday, "month", 1); // 添加Number对象
cJSON_AddNumberToObject(birthday, "day", 1); // 添加Number对象
output = cJSON_Print(root);
printf("%s\n", output);
cJSON_ReplaceItemInObject(birthday, "year", cJSON_CreateNumber(2000)); // 通过更换对象修改数据
cJSON_ReplaceItemInObject(birthday, "month", cJSON_CreateNumber(1));
cJSON_ReplaceItemInObject(birthday, "day", cJSON_CreateNumber(20));
output = cJSON_Print(root);
printf("%s\n", output);
// 释放内存不能少
free(output);
cJSON_Delete(root);
return 0;
}
总结(Conclusion)
本文主要介绍cJSON的一些使用方法,后续还会继续介绍更详细的内容。