隨著互聯網技術和移動智能設備的迅猛發展,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/zh-tw/n/232058.html