随着互联网技术和移动智能设备的迅猛发展,JSON(JavaScript Object Notation)已经成为了一种流行的数据传输和存储格式。在C语言中,CJSON是一种轻量级的JSON解析库,可以方便地解析和处理JSON格式数据。
一、CJSON库的基本使用
1、下载安装
git clone https://github.com/DaveGamble/cJSON.git cd cJSON mkdir build cd build cmake .. make sudo make install
2、解析JSON数据
#include <stdio.h>
#include <cjson/cJSON.h>
int main()
{
const char* json = "{ \"name\":\"John\", \"age\":20, \"isStudent\":true }";
cJSON* jsonRoot = cJSON_Parse(json);
char* name = cJSON_GetObjectItem(jsonRoot, "name")->valuestring;
int age = cJSON_GetObjectItem(jsonRoot, "age")->valueint;
int isStudent = cJSON_GetObjectItem(jsonRoot, "isStudent")->valueint;
printf("name: %s\nage: %d\nisStudent: %s", name, age, (isStudent == 1) ? "true" : "false");
cJSON_Delete(jsonRoot);
return 0;
}
3、生成JSON数据
#include <stdio.h>
#include <cjson/cJSON.h>
int main()
{
cJSON* jsonRoot = cJSON_CreateObject();
cJSON* jsonName = cJSON_CreateString("John");
cJSON* jsonAge = cJSON_CreateNumber(20);
cJSON* jsonIsStudent = cJSON_CreateBool(true);
cJSON_AddItemToObject(jsonRoot, "name", jsonName);
cJSON_AddItemToObject(jsonRoot, "age", jsonAge);
cJSON_AddItemToObject(jsonRoot, "isStudent", jsonIsStudent);
char* jsonStr = cJSON_PrintUnformatted(jsonRoot);
printf("jsonStr: %s", jsonStr);
cJSON_Delete(jsonRoot);
return 0;
}
二、CJSON库的高级使用
1、解析复杂JSON数据
#include <stdio.h>
#include <cjson/cJSON.h>
int main()
{
const char* json = "{ \"name\":\"John\", \"age\":20, \"isStudent\":true, \"contact\": { \"phone\": \"123456789\", \"email\": \"john@gmail.com\" }, \"hobbies\":[\"basketball\", \"reading\", \"swimming\"] }";
cJSON* jsonRoot = cJSON_Parse(json);
char* name = cJSON_GetObjectItem(jsonRoot, "name")->valuestring;
int age = cJSON_GetObjectItem(jsonRoot, "age")->valueint;
int isStudent = cJSON_GetObjectItem(jsonRoot, "isStudent")->valueint;
char* phone = cJSON_GetObjectItem(cJSON_GetObjectItem(jsonRoot, "contact"), "phone")->valuestring;
char* email = cJSON_GetObjectItem(cJSON_GetObjectItem(jsonRoot, "contact"), "email")->valuestring;
printf("name: %s\nage: %d\nisStudent: %s\nphone: %s\nemail: %s\n", name, age, (isStudent == 1) ? "true" : "false", phone, email);
cJSON* hobbies = cJSON_GetObjectItem(jsonRoot, "hobbies");
cJSON* hobby = NULL;
int index = 0;
cJSON_ArrayForEach(hobby, hobbies)
{
printf("hobby[%d]: %s\n", index, hobby->valuestring);
index++;
}
cJSON_Delete(jsonRoot);
return 0;
}
2、生成带格式化的JSON数据
#include <stdio.h>
#include <cjson/cJSON.h>
int main()
{
cJSON* jsonRoot = cJSON_CreateObject();
cJSON* jsonName = cJSON_CreateString("John");
cJSON* jsonAge = cJSON_CreateNumber(20);
cJSON* jsonIsStudent = cJSON_CreateBool(true);
cJSON* jsonContact = cJSON_CreateObject();
cJSON_AddItemToObject(jsonContact, "phone", cJSON_CreateString("123456789"));
cJSON_AddItemToObject(jsonContact, "email", cJSON_CreateString("john@gmail.com"));
cJSON* jsonHobbies = cJSON_CreateArray();
cJSON_AddItemToArray(jsonHobbies, cJSON_CreateString("basketball"));
cJSON_AddItemToArray(jsonHobbies, cJSON_CreateString("reading"));
cJSON_AddItemToArray(jsonHobbies, cJSON_CreateString("swimming"));
cJSON_AddItemToObject(jsonRoot, "name", jsonName);
cJSON_AddItemToObject(jsonRoot, "age", jsonAge);
cJSON_AddItemToObject(jsonRoot, "isStudent", jsonIsStudent);
cJSON_AddItemToObject(jsonRoot, "contact", jsonContact);
cJSON_AddItemToObject(jsonRoot, "hobbies", jsonHobbies);
char* jsonStr = cJSON_PrintIndented(jsonRoot);
printf("jsonStr: \n%s", jsonStr);
cJSON_Delete(jsonRoot);
return 0;
}
三、CJSON库的注意事项
1、使用完以后,要记得使用 cJSON_Delete函数清理生成的JSON对象。
2、解析JSON数据时,需要根据数据类型选择对应的解析函数,比如字符串、数字、数组等等。
3、生成JSON数据时,如果键值对中的键名已经存在,则会被覆盖。
4、在使用 cJSON_AddItemToArray函数将一个JSON对象添加到数组中时,要保证这个JSON对象没有被其他JSON对象引用,因为 cJSON_AddItemToArray函数内部会将添加的JSON对象的 next指针置为 NULL。
总之,CJSON是一款小巧、简单、易用的JSON解析库,可以方便地在C语言项目中使用,能够轻松地解决JSON数据的解析和生成问题。
原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/232058.html
微信扫一扫
支付宝扫一扫