CJSON:C語言中的輕量級JSON解析庫

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

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-11 12:49
下一篇 2024-12-11 12:49

相關推薦

  • AES加密解密演算法的C語言實現

    AES(Advanced Encryption Standard)是一種對稱加密演算法,可用於對數據進行加密和解密。在本篇文章中,我們將介紹C語言中如何實現AES演算法,並對實現過程進…

    編程 2025-04-29
  • 學習Python對學習C語言有幫助嗎?

    Python和C語言是兩種非常受歡迎的編程語言,在程序開發中都扮演著非常重要的角色。那麼,學習Python對學習C語言有幫助嗎?答案是肯定的。在本文中,我們將從多個角度探討Pyth…

    編程 2025-04-29
  • Python被稱為膠水語言

    Python作為一種跨平台的解釋性高級語言,最大的特點是被稱為”膠水語言”。 一、簡單易學 Python的語法簡單易學,更加人性化,這使得它成為了初學者的入…

    編程 2025-04-29
  • OpenJudge答案1.6的C語言實現

    本文將從多個方面詳細闡述OpenJudge答案1.6在C語言中的實現方法,幫助初學者更好地學習和理解。 一、需求概述 OpenJudge答案1.6的要求是,輸入兩個整數a和b,輸出…

    編程 2025-04-29
  • Python按位運算符和C語言

    本文將從多個方面詳細闡述Python按位運算符和C語言的相關內容,並給出相應的代碼示例。 一、概述 Python是一種動態的、面向對象的編程語言,其按位運算符是用於按位操作的運算符…

    編程 2025-04-29
  • JSON的MD5

    在Web開發過程中,JSON(JavaScript Object Notation)是最常用的數據格式之一。MD5(Message-Digest Algorithm 5)是一種常用…

    編程 2025-04-29
  • 使用Java將JSON寫入HDFS

    本篇文章將從以下幾個方面詳細闡述Java將JSON寫入HDFS的方法: 一、HDFS簡介 首先,先來了解一下Hadoop分散式文件系統(HDFS)。HDFS是一個可擴展性高的分散式…

    編程 2025-04-29
  • Python語言由荷蘭人為中心的全能編程開發工程師

    Python語言是一種高級語言,很多編程開發工程師都喜歡使用Python語言進行開發。Python語言的創始人是荷蘭人Guido van Rossum,他在1989年聖誕節期間開始…

    編程 2025-04-28
  • Python語言設計基礎第2版PDF

    Python語言設計基礎第2版PDF是一本介紹Python編程語言的經典教材。本篇文章將從多個方面對該教材進行詳細的闡述和介紹。 一、基礎知識 本教材中介紹了Python編程語言的…

    編程 2025-04-28
  • Python語言實現人名最多數統計

    本文將從幾個方面詳細介紹Python語言實現人名最多數統計的方法和應用。 一、Python實現人名最多數統計的基礎 1、首先,我們需要了解Python語言的一些基礎知識,如列表、字…

    編程 2025-04-28

發表回復

登錄後才能評論