JSON (JavaScript Object Notation) 是一种轻量级的数据交换格式,易于人阅读和编写,也易于机器解析和生成。
JSON 的基本数据类型
类型 | 描述 | 示例 |
|---|---|---|
对象 | 无序的键值对集合 |
|
数组 | 有序的值列表 |
|
字符串 | 双引号包围的文本 |
|
数字 | 整数或浮点数 |
|
布尔值 | true 或 false |
|
null | 空值 |
|
JSON 语法规则
数据以键值对形式存在
键必须是字符串(双引号)
值可以是任意JSON数据类型
对象用
{}包围,数组用[]包围数据之间用逗号分隔
例如:
{ "name": "Alice", "age": 25, "is_student": false, "courses": ["Math", "Science", "History"], "address": { "street": "123 Main St", "city": "New York" }, "scores": null }根据语法规则我们可以知道,"courses"对应的就是一个数组,"address"对应的就是一个对象...
我们在项目中手动建立一个json数据或者是解析json数据,当然也是有工具可以使用的,比如:cJSON库
1. cJSON 简介
cJSON 是一个超轻量级的JSON解析器和生成器,用C语言编写,只有单个源文件和头文件,易于集成到项目中。
2.cJSON 的主要函数
创建和删除函数
cJSON 的主要函数 创建和删除函数 // 创建JSON对象 cJSON *cJSON_CreateObject(void); // 创建JSON数组 cJSON *cJSON_CreateArray(void); // 创建字符串 cJSON *cJSON_CreateString(const char *string); // 创建数字 cJSON *cJSON_CreateNumber(double num); // 创建布尔值 cJSON *cJSON_CreateTrue(void); cJSON *cJSON_CreateFalse(void); cJSON *cJSON_CreateBool(int boolean); // 创建null cJSON *cJSON_CreateNull(void); // 删除JSON对象 void cJSON_Delete(cJSON *item);示例1:创建JSON对象
示例1:创建JSON对象 #include <stdio.h> #include "cJSON.h" int main() { // 创建根对象 cJSON *root = cJSON_CreateObject(); // 添加基本字段 cJSON_AddStringToObject(root, "name", "John Doe"); cJSON_AddNumberToObject(root, "age", 30); cJSON_AddBoolToObject(root, "is_student", 0); // 0=false, 1=true // 创建并添加数组 cJSON *hobbies = cJSON_CreateArray(); cJSON_AddItemToArray(hobbies, cJSON_CreateString("reading")); cJSON_AddItemToArray(hobbies, cJSON_CreateString("gaming")); cJSON_AddItemToArray(hobbies, cJSON_CreateString("coding")); cJSON_AddItemToObject(root, "hobbies", hobbies); // 创建并添加嵌套对象 cJSON *address = cJSON_CreateObject(); cJSON_AddStringToObject(address, "street", "123 Main St"); cJSON_AddStringToObject(address, "city", "Boston"); cJSON_AddStringToObject(address, "zipcode", "02101"); cJSON_AddItemToObject(root, "address", address); // 转换为字符串并打印 char *json_string = cJSON_Print(root); printf("Created JSON:\n%s\n", json_string); // 清理内存 free(json_string); cJSON_Delete(root); return 0; } 输出: { "name": "John Doe", "age": 30, "is_student": false, "hobbies": ["reading", "gaming", "coding"], "address": { "street": "123 Main St", "city": "Boston", "zipcode": "02101" } }数组操作函数 数组访问 // 获取数组大小 int cJSON_GetArraySize(const cJSON *array); // 根据索引获取数组项目 cJSON *cJSON_GetArrayItem(const cJSON *array, int index);对象访问函数 获取对象成员 // 根据键名获取对象中的项目 cJSON *cJSON_GetObjectItem(const cJSON *object, const char *string); // 根据键名获取对象中的项目(大小写不敏感) cJSON *cJSON_GetObjectItemCaseSensitive(const cJSON *object, const char *string); 安全获取函数(推荐) // 安全地获取对象项目并检查存在性 cJSON *cJSON_GetObjectItemSafe(const cJSON *object, const char *string); // 检查对象是否包含某个键 int cJSON_HasObjectItem(const cJSON *object, const char *string); 3. 类型检查函数 基本类型检查 // 检查是否为对象 int cJSON_IsObject(const cJSON *item); // 检查是否为数组 int cJSON_IsArray(const cJSON *item); // 检查是否为字符串 int cJSON_IsString(const cJSON *item); // 检查是否为数字 int cJSON_IsNumber(const cJSON *item); // 检查是否为布尔值 int cJSON_IsBool(const cJSON *item); // 检查是否为true int cJSON_IsTrue(const cJSON *item); // 检查是否为false int cJSON_IsFalse(const cJSON *item); // 检查是否为null int cJSON_IsNull(const cJSON *item); 复合类型检查 // 检查是否为原始类型(字符串、数字、布尔、null) int cJSON_IsRaw(const cJSON *item); // 检查是否为引用 int cJSON_IsReference(const cJSON *item); 4. 值获取函数 从已知类型获取值 // 从字符串项目获取字符串值 const char *cJSON_GetStringValue(const cJSON *item); // 从数字项目获取整数值 int cJSON_GetNumberValue(const cJSON *item); // 从数字项目获取双精度值 double cJSON_GetNumberDoubleValue(const cJSON *item);