使用 CJSON 在C語言中進行 JSON 的創建和解析的實例講解

本文用代碼簡單介紹cjson的使用方法,1)創建json,從json中獲取數據。2)創建json數組和解析json數組

 

1、 創建json,從json中獲取數據

複製代碼
  1 #include <stdio.h>                                                                                            
  2 #include "cJSON.h"
  3 
  4 char * makeJson()
  5 {
  6     cJSON * pJsonRoot = NULL;
  7 
  8     pJsonRoot = cJSON_CreateObject();
  9     if(NULL == pJsonRoot)
 10     {
 11         //error happend here
 12         return NULL;
 13     }
 14     cJSON_AddStringToObject(pJsonRoot, "hello", "hello world");
 15     cJSON_AddNumberToObject(pJsonRoot, "number", 10010);
 16     cJSON_AddBoolToObject(pJsonRoot, "bool", 1);
 17     cJSON * pSubJson = NULL;
 18     pSubJson = cJSON_CreateObject();
 19     if(NULL == pSubJson)
 20     {
 21         // create object faild, exit
 22         cJSON_Delete(pJsonRoot);
 23         return NULL;
 24     }
 25     cJSON_AddStringToObject(pSubJson, "subjsonobj", "a sub json string");
 26     cJSON_AddItemToObject(pJsonRoot, "subobj", pSubJson);
 27 
 28     char * p = cJSON_Print(pJsonRoot);
 29   // else use : 
 30     // char * p = cJSON_PrintUnformatted(pJsonRoot);
 31     if(NULL == p)
 32     {
 33         //convert json list to string faild, exit
 34         //because sub json pSubJson han been add to pJsonRoot, so just delete pJsonRoot, if you also delete pSubJson, it will coredump, and error is : double free
 35         cJSON_Delete(pJsonRoot);
 36         return NULL;
 37     }
 38     //free(p);
 39     
 40     cJSON_Delete(pJsonRoot);
 41 
 42     return p;
 43 }
 44 
 45 void parseJson(char * pMsg)
 46 {
 47     if(NULL == pMsg)
 48     {
 49         return;
 50     }
 51     cJSON * pJson = cJSON_Parse(pMsg);
 52     if(NULL == pJson)                                                                                         
 53     {
 54         // parse faild, return
 55       return ;
 56     }
 57 
 58     // get string from json
 59     cJSON * pSub = cJSON_GetObjectItem(pJson, "hello");
 60     if(NULL == pSub)
 61     {
 62         //get object named "hello" faild
 63     }
 64     printf("obj_1 : %s\n", pSub->valuestring);
 65 
 66     // get number from json
 67     pSub = cJSON_GetObjectItem(pJson, "number");
 68     if(NULL == pSub)
 69     {
 70         //get number from json faild
 71     }
 72     printf("obj_2 : %d\n", pSub->valueint);
 73 
 74     // get bool from json
 75     pSub = cJSON_GetObjectItem(pJson, "bool");
 76     if(NULL == pSub)
 77     {
 78         // get bool from json faild
 79     }                                                                                                         
 80     printf("obj_3 : %d\n", pSub->valueint);
 81 
 82  // get sub object
 83     pSub = cJSON_GetObjectItem(pJson, "subobj");
 84     if(NULL == pSub)
 85     {
 86         // get sub object faild
 87     }
 88     cJSON * pSubSub = cJSON_GetObjectItem(pSub, "subjsonobj");
 89     if(NULL == pSubSub)
 90     {
 91         // get object from subject object faild
 92     }
 93     printf("sub_obj_1 : %s\n", pSubSub->valuestring);
 94 
 95     cJSON_Delete(pJson);
 96 }
 97 
 98 int main()
 99 {
100     char * p = makeJson();
101     if(NULL == p)
102     {
103         return 0;
104     }
105     printf("%s\n", p);
106     parseJson(p);                                                                                             
107   free(p);  //千萬不要忘記釋放內存呀,cJSON_Print()函數或者cJSON_PrintUnformatted()產生的內存,使用free(char *)進行釋放
108     return 0;
109 }
複製代碼

  

centos下編譯通過,運行結果如下

複製代碼
 1 {
 2     "hello":    "hello world",
 3     "number":    10010,
 4     "bool":    true,
 5     "subobj":    {
 6         "subjsonobj":    "a sub json string"
 7     }
 8 }
 9 obj_1 : hello world
10 obj_2 : 10010
11 obj_3 : 1
12 sub_obj_1 : a sub json string
複製代碼

代碼解釋如下:

CJSON在內存中的存儲方式是用鏈表進行存儲的,所以在進行操作的時候,我們可見的部分全部是用指針進行操作的。

第8行新建一個JSON項目。

第14、15、16行分別添加了字符串、數字和bool變量。

第18行新建一個JSON項目:pSubJson。

第25行在新建的pSubJson項目上添加字符串。

第26行把我們的新項目添加到最初的項目pJsonRoot上。

第28行把CJSON的內存的存儲的數據轉換爲字符串格式。

cjson庫的 百度網盤 下載地址在:http://pan.baidu.com/s/1ntsRLgt

 

結果分析:

第1到8行爲創建的JSON字符串

第9到12行爲從JSON解析得到的數據

 

2、創建json數組和解析json數組

複製代碼
 1 //創建數組,數組值是另一個JSON的item,這裏使用數字作爲演示
 2 char * makeArray(int iSize)
 3 {
 4     cJSON * root =  cJSON_CreateArray();                                                             
 5     if(NULL == root)
 6     {
 7         printf("create json array faild\n");
 8         return NULL;
 9     }
10     int i = 0;
11     
12     for(i = 0; i < iSize; i++)
13     {
14         cJSON_AddNumberToObject(root, "hehe", i);
15     }
16     char * out = cJSON_Print(root);
17     cJSON_Delete(root);
18 
19     return out;
20 }
21 
22 //解析剛剛的CJSON數組
23 void parseArray(char * pJson)
24 {
25     if(NULL == pJson)
26     {                                                                                                
27         return ;
28     }
29     cJSON * root = NULL;
30     if((root = cJSON_Parse(pJson)) == NULL)
31     {
32         return ;
33     }
34     int iSize = cJSON_GetArraySize(root);
35     for(int iCnt = 0; iCnt < iSize; iCnt++)
36     {
37         cJSON * pSub = cJSON_GetArrayItem(root, iCnt);
38         if(NULL == pSub)
39         {
40             continue;
41         }
42         int iValue = pSub->valueint;
43         printf("value[%2d] : [%d]\n", iCnt, iValue);
44     }   
45     cJSON_Delete(root);
46     return;
47 }
複製代碼

輸出結果

1)創建JSON數組

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

2)解析數組的輸出結果

複製代碼
value[ 0] : [0]
value[ 1] : [1]
value[ 2] : [2]
value[ 3] : [3]
value[ 4] : [4]
value[ 5] : [5]
value[ 6] : [6]
value[ 7] : [7]
value[ 8] : [8]
value[ 9] : [9]
複製代碼
發佈了26 篇原創文章 · 獲贊 39 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章