自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

JSON簡介及C代碼展示JSON消息示例

開發(fā) 開發(fā)工具
本文首先對JSON進(jìn)行簡單的介紹,然后用具體的C代碼示范了各類JSON消息的構(gòu)造方法。

在互聯(lián)網(wǎng)軟件前端與后臺(tái)進(jìn)行消息交互的過程中,需要有一種標(biāo)準(zhǔn)的數(shù)據(jù)交換格式供前后端采用。在眾多的數(shù)據(jù)交換格式中,JSON(JavaScript Object Notation,JS 對象標(biāo)記)是應(yīng)用得比較廣泛的,它采用完全獨(dú)立于編程語言的文本格式來存儲(chǔ)和表示數(shù)據(jù)。JSON的層次結(jié)構(gòu)簡潔、清晰,易于閱讀和編寫,同時(shí)也易于機(jī)器解析和生成,這有效地提升了網(wǎng)絡(luò)傳輸效率。

[[192401]]

本文首先對JSON進(jìn)行簡單的介紹,然后用具體的C代碼示范了各類JSON消息的構(gòu)造方法。

JSON簡介

JSON 的語法規(guī)則可以用下面的四句話來概括:

  • ***,對象表示為鍵值對。
  • 第二,數(shù)據(jù)由逗號(hào)分隔。
  • 第三,花括號(hào)保存對象。
  • 第四,方括號(hào)保存數(shù)組。

具體而言,鍵值對組合中的鍵名寫在前面并用雙引號(hào)包裹,鍵值使用冒號(hào)分隔,冒號(hào)后面緊接著值,如:”name”: “zhou”;數(shù)組是用方括號(hào)包裹起來的,如:[“zhou”, “zhang”]。

JSON消息示例

本部分用實(shí)際的C代碼來示范了各類常用的JSON消息的構(gòu)造方法。在編寫代碼之前,要到https://sourceforge.net/projects/cjson/上去下載C語言版的JSON封裝API。

在JSON的API中,我們常用到的有如下幾個(gè)函數(shù):

1)cJSON_CreateObject():創(chuàng)建JSON對象。

2)cJSON_Delete(cJSON *c):刪除一個(gè)JSON結(jié)構(gòu)。

3)cJSON_AddStringToObject(object,name,s):將一個(gè)字符串添加到對象中。

4)cJSON_AddNumberToObject(object,name,n):將一個(gè)整數(shù)添加到對象中。

5)cJSON_Print(cJSON *item):將JSON消息以文本消息的樣式輸出。

6)cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item):將一個(gè)數(shù)據(jù)(通常為對象)添加到一個(gè)對象中。

7)cJSON_CreateString(const char *string):生成字符串?dāng)?shù)據(jù)。

8)cJSON_AddItemToArray(cJSON *array, cJSON *item):將一個(gè)數(shù)據(jù)添加到一個(gè)數(shù)組中。

9)cJSON_CreateArray():創(chuàng)建JSON數(shù)組。

下面,我們開始編寫C代碼來生成JSON消息。

1. 如果要實(shí)現(xiàn)如下JSON消息:

  1.     name:"zhou", 
  2.     age:30 

則編寫C代碼函數(shù)如下:

  1. int MakeJsonNameAge(char *pszJsonContent, int iJsonLen) 
  2.     cJSON *root = NULL
  3.     char  *out  = NULL
  4.  
  5.     // 判斷函數(shù)參數(shù)是否合法 
  6.     if (pszJsonContent == NULL) 
  7.     { 
  8.         printf("MakeJsonNameAge: pszJsonContent is NULL!"); 
  9.  
  10.         return -1; 
  11.     } 
  12.  
  13.     root = cJSON_CreateObject(); 
  14.     if(NULL == root) 
  15.     { 
  16.         printf("MakeJsonNameAge: exec cJSON_CreateObject to get root failed!"); 
  17.  
  18.         return -1; 
  19.     } 
  20.  
  21.     cJSON_AddStringToObject(root, "name", "zhou"); 
  22.  
  23.     cJSON_AddNumberToObject(root, "age", 30); 
  24.  
  25.     out=cJSON_Print(root); 
  26.     strncpy(pszJsonContent, out, iJsonLen - 1); 
  27.     pszJsonContent[iJsonLen - 1] = '\0'; 
  28.  
  29.     cJSON_Delete(root); 
  30.     free(out); 
  31.  
  32.     return 0; 

2. 如果要實(shí)現(xiàn)如下JSON消息:

  1.     personinfo:{ 
  2.         name:"zhou", 
  3.         age:30 
  4.     } 

則編寫C代碼函數(shù)如下:

  1. int MakeJsonPersonInfo(char *pszJsonContent, int iJsonLen) 
  2.     cJSON *root        = NULL
  3.     cJSON *JsonLevel1  = NULL
  4.     char  *out         = NULL
  5.  
  6.     // 判斷函數(shù)參數(shù)是否合法 
  7.     if (pszJsonContent == NULL) 
  8.     { 
  9.         printf("MakeJsonPersonInfo: pszJsonContent is NULL!"); 
  10.  
  11.         return -1; 
  12.     } 
  13.  
  14.     root = cJSON_CreateObject(); 
  15.     if(NULL == root) 
  16.     { 
  17.         printf("MakeJsonPersonInfo: exec cJSON_CreateObject to get root failed!"); 
  18.  
  19.         return -1; 
  20.     } 
  21.  
  22.     JsonLevel1 = cJSON_CreateObject(); 
  23.     if(NULL == JsonLevel1) 
  24.     { 
  25.         printf("MakeJsonPersonInfo: exec cJSON_CreateObject to get JsonLevel1 failed!"); 
  26.  
  27.         cJSON_Delete(root); 
  28.         return -1; 
  29.     }    
  30.  
  31.     cJSON_AddStringToObject(JsonLevel1, "name", "zhou"); 
  32.  
  33.     cJSON_AddNumberToObject(JsonLevel1, "age", 30); 
  34.  
  35.     cJSON_AddItemToObject(root, "personinfo", JsonLevel1); 
  36.  
  37.     out=cJSON_Print(root); 
  38.     strncpy(pszJsonContent, out, iJsonLen - 1); 
  39.     pszJsonContent[iJsonLen - 1] = '\0'; 
  40.  
  41.     cJSON_Delete(root); 
  42.     free(out); 
  43.  
  44.     return 0; 

3. 如果要實(shí)現(xiàn)如下JSON消息:

  1.     personinfo1:{ 
  2.         name:"zhou", 
  3.         age:30 
  4.     }, 
  5.     personinfo2:{ 
  6.         name:"zhang", 
  7.         age:41 
  8.     } 

 則編寫C代碼函數(shù)如下:

  1. int MakeJsonTwoPersonInfo(char *pszJsonContent, int iJsonLen) 
  2.     cJSON *root        = NULL
  3.     cJSON *JsonLevel1  = NULL
  4.     char  *out         = NULL
  5.  
  6.     // 判斷函數(shù)參數(shù)是否合法 
  7.     if (pszJsonContent == NULL) 
  8.     { 
  9.         printf("MakeJsonTwoPersonInfo: pszJsonContent is NULL!"); 
  10.  
  11.         return -1; 
  12.     } 
  13.  
  14.     root = cJSON_CreateObject(); 
  15.     if(NULL == root) 
  16.     { 
  17.         printf("MakeJsonTwoPersonInfo: exec cJSON_CreateObject to get root failed!"); 
  18.  
  19.         return -1; 
  20.     } 
  21.  
  22.     //--------------- 
  23.     JsonLevel1 = cJSON_CreateObject(); 
  24.     if(NULL == JsonLevel1) 
  25.     { 
  26.         printf("MakeJsonTwoPersonInfo: exec cJSON_CreateObject to get JsonLevel1 failed 1!"); 
  27.  
  28.         cJSON_Delete(root); 
  29.         return -1; 
  30.     }    
  31.  
  32.     cJSON_AddStringToObject(JsonLevel1, "name", "zhou"); 
  33.  
  34.     cJSON_AddNumberToObject(JsonLevel1, "age", 30); 
  35.  
  36.     cJSON_AddItemToObject(root, "personinfo1", JsonLevel1); 
  37.  
  38.     //--------------- 
  39.     JsonLevel1 = cJSON_CreateObject(); 
  40.     if(NULL == JsonLevel1) 
  41.     { 
  42.         printf("MakeJsonTwoPersonInfo: exec cJSON_CreateObject to get JsonLevel1 failed 2!"); 
  43.  
  44.         cJSON_Delete(root); 
  45.         return -1; 
  46.     }    
  47.  
  48.     cJSON_AddStringToObject(JsonLevel1, "name", "zhang"); 
  49.  
  50.     cJSON_AddNumberToObject(JsonLevel1, "age", 40); 
  51.  
  52.     cJSON_AddItemToObject(root, "personinfo2", JsonLevel1); 
  53.  
  54.     out=cJSON_Print(root); 
  55.     strncpy(pszJsonContent, out, iJsonLen - 1); 
  56.     pszJsonContent[iJsonLen - 1] = '\0'; 
  57.  
  58.     cJSON_Delete(root); 
  59.     free(out); 
  60.  
  61.     return 0; 

4. 如果要實(shí)現(xiàn)如下JSON消息:

  1.     id:"123456", 
  2.     personinfo:{ 
  3.         name:"zhou", 
  4.         age:30 
  5.     } 

則編寫C代碼函數(shù)如下:

  1. int MakeJsonIDPersonInfo(char *pszJsonContent, int iJsonLen) 
  2.     cJSON *root        = NULL
  3.     cJSON *JsonLevel1  = NULL
  4.     char  *out         = NULL
  5.  
  6.     // 判斷函數(shù)參數(shù)是否合法 
  7.     if (pszJsonContent == NULL) 
  8.     { 
  9.         printf("MakeJsonIDPersonInfo: pszJsonContent is NULL!"); 
  10.  
  11.         return -1; 
  12.     } 
  13.  
  14.     root = cJSON_CreateObject(); 
  15.     if(NULL == root) 
  16.     { 
  17.         printf("MakeJsonIDPersonInfo: exec cJSON_CreateObject to get root failed!"); 
  18.  
  19.         return -1; 
  20.     } 
  21.  
  22.     cJSON_AddStringToObject(root, "id", "123456"); 
  23.  
  24.     JsonLevel1 = cJSON_CreateObject(); 
  25.     if(NULL == JsonLevel1) 
  26.     { 
  27.         printf("MakeJsonIDPersonInfo: exec cJSON_CreateObject to get JsonLevel1 failed!"); 
  28.  
  29.         cJSON_Delete(root); 
  30.         return -1; 
  31.     }    
  32.  
  33.     cJSON_AddStringToObject(JsonLevel1, "name", "zhou"); 
  34.  
  35.     cJSON_AddNumberToObject(JsonLevel1, "age", 30); 
  36.  
  37.     cJSON_AddItemToObject(root, "personinfo", JsonLevel1); 
  38.  
  39.     out=cJSON_Print(root); 
  40.     strncpy(pszJsonContent, out, iJsonLen - 1); 
  41.     pszJsonContent[iJsonLen - 1] = '\0'; 
  42.  
  43.     cJSON_Delete(root); 
  44.     free(out); 
  45.  
  46.     return 0; 

5. 如果要實(shí)現(xiàn)如下JSON消息:

  1.     personname:[ 
  2.         "zhou", 
  3.         "zhang" 
  4.     ] 

則編寫C代碼函數(shù)如下:

  1. int MakeJsonPersonNameInfo(char *pszJsonContent, int iJsonLen) 
  2.     cJSON *root        = NULL
  3.     cJSON *JsonLevel1  = NULL
  4.     cJSON *JsonLevel2  = NULL
  5.     char  *out         = NULL
  6.  
  7.     // 判斷函數(shù)參數(shù)是否合法 
  8.     if (pszJsonContent == NULL) 
  9.     { 
  10.         printf("MakeJsonPersonNameInfo: pszJsonContent is NULL!"); 
  11.  
  12.         return -1; 
  13.     } 
  14.  
  15.     root = cJSON_CreateObject(); 
  16.     if (NULL == root) 
  17.     { 
  18.         printf("MakeJsonPersonNameInfo: exec cJSON_CreateObject to get root failed!"); 
  19.  
  20.         return -1; 
  21.     } 
  22.  
  23.     JsonLevel1 = cJSON_CreateArray(); 
  24.     if (NULL == JsonLevel1) 
  25.     { 
  26.         printf("MakeJsonPersonNameInfo: exec cJSON_CreateArray to get JsonLevel1 failed!"); 
  27.  
  28.         cJSON_Delete(root); 
  29.         return -1; 
  30.     } 
  31.  
  32.     cJSON_AddItemToObject(root, "personname", JsonLevel1); 
  33.  
  34.     JsonLevel2 = cJSON_CreateString("zhou"); 
  35.     cJSON_AddItemToArray(JsonLevel1, JsonLevel2); 
  36.  
  37.     JsonLevel2 = cJSON_CreateString("zhang"); 
  38.     cJSON_AddItemToArray(JsonLevel1, JsonLevel2); 
  39.  
  40.     out=cJSON_Print(root); 
  41.     strncpy(pszJsonContent, out, iJsonLen - 1); 
  42.     pszJsonContent[iJsonLen - 1] = '\0'; 
  43.  
  44.     cJSON_Delete(root); 
  45.     free(out); 
  46.  
  47.     return 0; 

6. 如果要實(shí)現(xiàn)如下JSON消息:

  1.     id:"123456", 
  2.     personname:[ 
  3.         "zhou", 
  4.         "zhang" 
  5.     ], 
  6.     personinfo:{ 
  7.         phonenumber:"15696192591", 
  8.         age:30 
  9.     } 

 則編寫C代碼函數(shù)如下:

  1. int MakeJsonIDPersonNameInfo(char *pszJsonContent, int iJsonLen) 
  2.     cJSON *root        = NULL
  3.     cJSON *JsonLevel1  = NULL
  4.     cJSON *JsonLevel2  = NULL
  5.     char  *out         = NULL
  6.  
  7.     // 判斷函數(shù)參數(shù)是否合法 
  8.     if (pszJsonContent == NULL) 
  9.     { 
  10.         printf("MakeJsonIDPersonNameInfo: pszJsonContent is NULL!"); 
  11.  
  12.         return -1; 
  13.     } 
  14.  
  15.     root = cJSON_CreateObject(); 
  16.     if (NULL == root) 
  17.     { 
  18.         printf("MakeJsonIDPersonNameInfo: exec cJSON_CreateObject to get root failed!"); 
  19.  
  20.         return -1; 
  21.     } 
  22.  
  23.     cJSON_AddStringToObject(root, "id", "123456"); 
  24.  
  25.     JsonLevel1 = cJSON_CreateArray(); 
  26.     if (NULL == JsonLevel1) 
  27.     { 
  28.         printf("MakeJsonIDPersonNameInfo: exec cJSON_CreateArray to get JsonLevel1 failed 1!"); 
  29.  
  30.         cJSON_Delete(root); 
  31.         return -1; 
  32.     } 
  33.  
  34.     cJSON_AddItemToObject(root, "personname", JsonLevel1); 
  35.  
  36.     JsonLevel2 = cJSON_CreateString("zhou"); 
  37.     cJSON_AddItemToArray(JsonLevel1, JsonLevel2); 
  38.  
  39.     JsonLevel2 = cJSON_CreateString("zhang"); 
  40.     cJSON_AddItemToArray(JsonLevel1, JsonLevel2); 
  41.  
  42.     //----------------- 
  43.     JsonLevel1 = cJSON_CreateObject(); 
  44.     if(NULL == JsonLevel1) 
  45.     { 
  46.         printf("MakeJsonIDPersonNameInfo: exec cJSON_CreateObject to get JsonLevel1 failed 2!"); 
  47.  
  48.         cJSON_Delete(root); 
  49.         return -1; 
  50.     }    
  51.  
  52.     cJSON_AddStringToObject(JsonLevel1, "name", "zhou"); 
  53.  
  54.     cJSON_AddNumberToObject(JsonLevel1, "age", 30); 
  55.  
  56.     cJSON_AddItemToObject(root, "personinfo", JsonLevel1); 
  57.  
  58.     out=cJSON_Print(root); 
  59.     strncpy(pszJsonContent, out, iJsonLen - 1); 
  60.     pszJsonContent[iJsonLen - 1] = '\0'; 
  61.  
  62.     cJSON_Delete(root); 
  63.     free(out); 
  64.  
  65.     return 0; 

7. 如果要實(shí)現(xiàn)如下JSON消息:

  1.     personinfo:{ 
  2.         personname:[ 
  3.         "zhou", 
  4.         "zhang" 
  5.         ], 
  6.         age:30 
  7.     } 

則編寫C代碼函數(shù)如下:

  1. int MakeJsonAgePersonNameInfo(char *pszJsonContent, int iJsonLen) 
  2.     cJSON *root        = NULL
  3.     cJSON *JsonLevel1  = NULL
  4.     cJSON *JsonLevel2  = NULL
  5.     cJSON *JsonLevel3  = NULL
  6.     char  *out         = NULL
  7.  
  8.     // 判斷函數(shù)參數(shù)是否合法 
  9.     if (pszJsonContent == NULL) 
  10.     { 
  11.         printf("MakeJsonAgePersonNameInfo: pszJsonContent is NULL!"); 
  12.  
  13.         return -1; 
  14.     } 
  15.  
  16.     root = cJSON_CreateObject(); 
  17.     if (NULL == root) 
  18.     { 
  19.         printf("MakeJsonAgePersonNameInfo: exec cJSON_CreateObject to get root failed!"); 
  20.  
  21.         return -1; 
  22.     } 
  23.  
  24.     JsonLevel1 = cJSON_CreateObject(); 
  25.     if(NULL == JsonLevel1) 
  26.     { 
  27.         printf("MakeJsonAgePersonNameInfo: exec cJSON_CreateObject to get JsonLevel1 failed!"); 
  28.  
  29.         cJSON_Delete(root); 
  30.         return -1; 
  31.     }    
  32.  
  33.     cJSON_AddItemToObject(root, "personinfo", JsonLevel1); 
  34.  
  35.     //------------------ 
  36.     JsonLevel2 = cJSON_CreateArray(); 
  37.     if (NULL == JsonLevel2) 
  38.     { 
  39.         printf("MakeJsonAgePersonNameInfo: exec cJSON_CreateArray to get JsonLevel2 failed!"); 
  40.  
  41.         cJSON_Delete(root); 
  42.         return -1; 
  43.     } 
  44.  
  45.     cJSON_AddItemToObject(JsonLevel1, "personname", JsonLevel2); 
  46.  
  47.     JsonLevel3 = cJSON_CreateString("zhou"); 
  48.     cJSON_AddItemToArray(JsonLevel2, JsonLevel3); 
  49.  
  50.     JsonLevel3 = cJSON_CreateString("zhang"); 
  51.     cJSON_AddItemToArray(JsonLevel2, JsonLevel3); 
  52.  
  53.     //------------------ 
  54.     cJSON_AddNumberToObject(JsonLevel1, "age", 30); 
  55.  
  56.  
  57.     out=cJSON_Print(root); 
  58.     strncpy(pszJsonContent, out, iJsonLen - 1); 
  59.     pszJsonContent[iJsonLen - 1] = '\0'; 
  60.  
  61.     cJSON_Delete(root); 
  62.     free(out); 
  63.  
  64.     return 0; 

8. 如果要實(shí)現(xiàn)如下JSON消息:

  1.     personinfo:[ 
  2.     { 
  3.         name:"zhou", 
  4.         age:30 
  5.     }, 
  6.     { 
  7.         name:"zhang", 
  8.         age:41 
  9.     } 
  10.     ] 

則編寫C代碼函數(shù)如下:

  1. int MakeJsonPersonsInfo(char *pszJsonContent, int iJsonLen) 
  2.     cJSON *root        = NULL
  3.     cJSON *JsonLevel1  = NULL
  4.     cJSON *JsonLevel2  = NULL
  5.     char  *out         = NULL
  6.  
  7.     // 判斷函數(shù)參數(shù)是否合法 
  8.     if (pszJsonContent == NULL) 
  9.     { 
  10.         printf("MakeJsonPersonsInfo: pszJsonContent is NULL!"); 
  11.  
  12.         return -1; 
  13.     } 
  14.  
  15.     root = cJSON_CreateObject(); 
  16.     if (NULL == root) 
  17.     { 
  18.         printf("MakeJsonPersonsInfo: exec cJSON_CreateObject to get root failed!"); 
  19.  
  20.         return -1; 
  21.     } 
  22.  
  23.     JsonLevel1 = cJSON_CreateArray(); 
  24.     if (NULL == JsonLevel1) 
  25.     { 
  26.         printf("MakeJsonPersonsInfo: exec cJSON_CreateArray to get JsonLevel1 failed!"); 
  27.  
  28.         cJSON_Delete(root); 
  29.         return -1; 
  30.     } 
  31.  
  32.     cJSON_AddItemToObject(root, "personinfo", JsonLevel1); 
  33.  
  34.     //--------------- 
  35.     JsonLevel2 = cJSON_CreateObject(); 
  36.     if(NULL == JsonLevel2) 
  37.     { 
  38.         printf("MakeJsonPersonsInfo: exec cJSON_CreateObject to get JsonLevel2 failed 1!"); 
  39.  
  40.         cJSON_Delete(root); 
  41.         return -1; 
  42.     }    
  43.  
  44.     cJSON_AddItemToArray(JsonLevel1, JsonLevel2); 
  45.  
  46.     cJSON_AddStringToObject(JsonLevel2, "name", "zhou"); 
  47.  
  48.     cJSON_AddNumberToObject(JsonLevel2, "age", 30); 
  49.  
  50.     //--------------- 
  51.     JsonLevel2 = cJSON_CreateObject(); 
  52.     if(NULL == JsonLevel2) 
  53.     { 
  54.         printf("MakeJsonPersonsInfo: exec cJSON_CreateObject to get JsonLevel2 failed 2!"); 
  55.  
  56.         cJSON_Delete(root); 
  57.         return -1; 
  58.     }    
  59.  
  60.     cJSON_AddItemToArray(JsonLevel1, JsonLevel2); 
  61.  
  62.     cJSON_AddStringToObject(JsonLevel2, "name", "zhang"); 
  63.  
  64.     cJSON_AddNumberToObject(JsonLevel2, "age", 41);      
  65.  
  66.     //--------------- 
  67.     out=cJSON_Print(root); 
  68.     strncpy(pszJsonContent, out, iJsonLen - 1); 
  69.     pszJsonContent[iJsonLen - 1] = '\0'; 
  70.  
  71.     cJSON_Delete(root); 
  72.     free(out); 
  73.  
  74.     return 0; 

總結(jié) 

以上是常見JSON消息的C代碼實(shí)現(xiàn)方法,大家可以編寫測試代碼來看最終生成的JSON消息是否是我們描述的那樣。我編寫了一個(gè)完整的測試代碼,放到了GitHub上,歡迎下載閱讀:https://github.com/zhouzxi/TestJson。(本測試程序是運(yùn)行在Linux上的,大家可以使用這個(gè)命令進(jìn)行編譯:

  1. gcc -g -o TestJson TestJson.c cJSON.c -pthread -lc -lm) 

【本文是51CTO專欄作者周兆熊的原創(chuàng)文章,作者微信公眾號(hào):周氏邏輯(logiczhou)】

戳這里,看該作者更多好文

責(zé)任編輯:趙寧寧 來源: 51CTO專欄
相關(guān)推薦

2020-07-20 07:56:28

JavaScript開發(fā)技術(shù)

2011-07-01 15:28:26

PhoneGap代碼示例

2019-07-16 08:38:34

JavaJson庫數(shù)據(jù)

2015-07-02 10:37:32

C#Json字符串類代碼

2010-01-06 17:06:05

Json格式

2016-12-20 11:12:11

C代碼自測開發(fā)

2009-08-27 15:53:30

C#中using wo

2023-11-12 11:56:28

Json格式弊端

2024-06-18 08:37:25

場景異步編程代碼

2024-06-24 03:00:00

2016-12-12 12:37:45

結(jié)構(gòu)C代碼賦值

2010-02-05 10:23:09

C++基本函數(shù)

2009-08-17 17:36:08

C# 枚舉

2014-07-10 10:09:11

JSON數(shù)據(jù)行轉(zhuǎn)列

2009-09-01 16:49:56

C#文件上傳下載

2009-07-03 17:44:06

JSP介紹

2009-09-16 16:32:20

JavaScript靜

2010-02-03 10:05:48

C++ enum枚舉

2009-08-24 08:56:55

C#反射

2023-09-13 14:42:08

typedefC++
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)