JSON簡介及C代碼展示JSON消息示例
在互聯(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ò)傳輸效率。
本文首先對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消息:
- {
- name:"zhou",
- age:30
- }
則編寫C代碼函數(shù)如下:
- int MakeJsonNameAge(char *pszJsonContent, int iJsonLen)
- {
- cJSON *root = NULL;
- char *out = NULL;
- // 判斷函數(shù)參數(shù)是否合法
- if (pszJsonContent == NULL)
- {
- printf("MakeJsonNameAge: pszJsonContent is NULL!");
- return -1;
- }
- root = cJSON_CreateObject();
- if(NULL == root)
- {
- printf("MakeJsonNameAge: exec cJSON_CreateObject to get root failed!");
- return -1;
- }
- cJSON_AddStringToObject(root, "name", "zhou");
- cJSON_AddNumberToObject(root, "age", 30);
- out=cJSON_Print(root);
- strncpy(pszJsonContent, out, iJsonLen - 1);
- pszJsonContent[iJsonLen - 1] = '\0';
- cJSON_Delete(root);
- free(out);
- return 0;
- }
2. 如果要實(shí)現(xiàn)如下JSON消息:
- {
- personinfo:{
- name:"zhou",
- age:30
- }
- }
則編寫C代碼函數(shù)如下:
- int MakeJsonPersonInfo(char *pszJsonContent, int iJsonLen)
- {
- cJSON *root = NULL;
- cJSON *JsonLevel1 = NULL;
- char *out = NULL;
- // 判斷函數(shù)參數(shù)是否合法
- if (pszJsonContent == NULL)
- {
- printf("MakeJsonPersonInfo: pszJsonContent is NULL!");
- return -1;
- }
- root = cJSON_CreateObject();
- if(NULL == root)
- {
- printf("MakeJsonPersonInfo: exec cJSON_CreateObject to get root failed!");
- return -1;
- }
- JsonLevel1 = cJSON_CreateObject();
- if(NULL == JsonLevel1)
- {
- printf("MakeJsonPersonInfo: exec cJSON_CreateObject to get JsonLevel1 failed!");
- cJSON_Delete(root);
- return -1;
- }
- cJSON_AddStringToObject(JsonLevel1, "name", "zhou");
- cJSON_AddNumberToObject(JsonLevel1, "age", 30);
- cJSON_AddItemToObject(root, "personinfo", JsonLevel1);
- out=cJSON_Print(root);
- strncpy(pszJsonContent, out, iJsonLen - 1);
- pszJsonContent[iJsonLen - 1] = '\0';
- cJSON_Delete(root);
- free(out);
- return 0;
- }
3. 如果要實(shí)現(xiàn)如下JSON消息:
- {
- personinfo1:{
- name:"zhou",
- age:30
- },
- personinfo2:{
- name:"zhang",
- age:41
- }
- }
則編寫C代碼函數(shù)如下:
- int MakeJsonTwoPersonInfo(char *pszJsonContent, int iJsonLen)
- {
- cJSON *root = NULL;
- cJSON *JsonLevel1 = NULL;
- char *out = NULL;
- // 判斷函數(shù)參數(shù)是否合法
- if (pszJsonContent == NULL)
- {
- printf("MakeJsonTwoPersonInfo: pszJsonContent is NULL!");
- return -1;
- }
- root = cJSON_CreateObject();
- if(NULL == root)
- {
- printf("MakeJsonTwoPersonInfo: exec cJSON_CreateObject to get root failed!");
- return -1;
- }
- //---------------
- JsonLevel1 = cJSON_CreateObject();
- if(NULL == JsonLevel1)
- {
- printf("MakeJsonTwoPersonInfo: exec cJSON_CreateObject to get JsonLevel1 failed 1!");
- cJSON_Delete(root);
- return -1;
- }
- cJSON_AddStringToObject(JsonLevel1, "name", "zhou");
- cJSON_AddNumberToObject(JsonLevel1, "age", 30);
- cJSON_AddItemToObject(root, "personinfo1", JsonLevel1);
- //---------------
- JsonLevel1 = cJSON_CreateObject();
- if(NULL == JsonLevel1)
- {
- printf("MakeJsonTwoPersonInfo: exec cJSON_CreateObject to get JsonLevel1 failed 2!");
- cJSON_Delete(root);
- return -1;
- }
- cJSON_AddStringToObject(JsonLevel1, "name", "zhang");
- cJSON_AddNumberToObject(JsonLevel1, "age", 40);
- cJSON_AddItemToObject(root, "personinfo2", JsonLevel1);
- out=cJSON_Print(root);
- strncpy(pszJsonContent, out, iJsonLen - 1);
- pszJsonContent[iJsonLen - 1] = '\0';
- cJSON_Delete(root);
- free(out);
- return 0;
- }
4. 如果要實(shí)現(xiàn)如下JSON消息:
- {
- id:"123456",
- personinfo:{
- name:"zhou",
- age:30
- }
- }
則編寫C代碼函數(shù)如下:
- int MakeJsonIDPersonInfo(char *pszJsonContent, int iJsonLen)
- {
- cJSON *root = NULL;
- cJSON *JsonLevel1 = NULL;
- char *out = NULL;
- // 判斷函數(shù)參數(shù)是否合法
- if (pszJsonContent == NULL)
- {
- printf("MakeJsonIDPersonInfo: pszJsonContent is NULL!");
- return -1;
- }
- root = cJSON_CreateObject();
- if(NULL == root)
- {
- printf("MakeJsonIDPersonInfo: exec cJSON_CreateObject to get root failed!");
- return -1;
- }
- cJSON_AddStringToObject(root, "id", "123456");
- JsonLevel1 = cJSON_CreateObject();
- if(NULL == JsonLevel1)
- {
- printf("MakeJsonIDPersonInfo: exec cJSON_CreateObject to get JsonLevel1 failed!");
- cJSON_Delete(root);
- return -1;
- }
- cJSON_AddStringToObject(JsonLevel1, "name", "zhou");
- cJSON_AddNumberToObject(JsonLevel1, "age", 30);
- cJSON_AddItemToObject(root, "personinfo", JsonLevel1);
- out=cJSON_Print(root);
- strncpy(pszJsonContent, out, iJsonLen - 1);
- pszJsonContent[iJsonLen - 1] = '\0';
- cJSON_Delete(root);
- free(out);
- return 0;
- }
5. 如果要實(shí)現(xiàn)如下JSON消息:
- {
- personname:[
- "zhou",
- "zhang"
- ]
- }
則編寫C代碼函數(shù)如下:
- int MakeJsonPersonNameInfo(char *pszJsonContent, int iJsonLen)
- {
- cJSON *root = NULL;
- cJSON *JsonLevel1 = NULL;
- cJSON *JsonLevel2 = NULL;
- char *out = NULL;
- // 判斷函數(shù)參數(shù)是否合法
- if (pszJsonContent == NULL)
- {
- printf("MakeJsonPersonNameInfo: pszJsonContent is NULL!");
- return -1;
- }
- root = cJSON_CreateObject();
- if (NULL == root)
- {
- printf("MakeJsonPersonNameInfo: exec cJSON_CreateObject to get root failed!");
- return -1;
- }
- JsonLevel1 = cJSON_CreateArray();
- if (NULL == JsonLevel1)
- {
- printf("MakeJsonPersonNameInfo: exec cJSON_CreateArray to get JsonLevel1 failed!");
- cJSON_Delete(root);
- return -1;
- }
- cJSON_AddItemToObject(root, "personname", JsonLevel1);
- JsonLevel2 = cJSON_CreateString("zhou");
- cJSON_AddItemToArray(JsonLevel1, JsonLevel2);
- JsonLevel2 = cJSON_CreateString("zhang");
- cJSON_AddItemToArray(JsonLevel1, JsonLevel2);
- out=cJSON_Print(root);
- strncpy(pszJsonContent, out, iJsonLen - 1);
- pszJsonContent[iJsonLen - 1] = '\0';
- cJSON_Delete(root);
- free(out);
- return 0;
- }
6. 如果要實(shí)現(xiàn)如下JSON消息:
- {
- id:"123456",
- personname:[
- "zhou",
- "zhang"
- ],
- personinfo:{
- phonenumber:"15696192591",
- age:30
- }
- }
則編寫C代碼函數(shù)如下:
- int MakeJsonIDPersonNameInfo(char *pszJsonContent, int iJsonLen)
- {
- cJSON *root = NULL;
- cJSON *JsonLevel1 = NULL;
- cJSON *JsonLevel2 = NULL;
- char *out = NULL;
- // 判斷函數(shù)參數(shù)是否合法
- if (pszJsonContent == NULL)
- {
- printf("MakeJsonIDPersonNameInfo: pszJsonContent is NULL!");
- return -1;
- }
- root = cJSON_CreateObject();
- if (NULL == root)
- {
- printf("MakeJsonIDPersonNameInfo: exec cJSON_CreateObject to get root failed!");
- return -1;
- }
- cJSON_AddStringToObject(root, "id", "123456");
- JsonLevel1 = cJSON_CreateArray();
- if (NULL == JsonLevel1)
- {
- printf("MakeJsonIDPersonNameInfo: exec cJSON_CreateArray to get JsonLevel1 failed 1!");
- cJSON_Delete(root);
- return -1;
- }
- cJSON_AddItemToObject(root, "personname", JsonLevel1);
- JsonLevel2 = cJSON_CreateString("zhou");
- cJSON_AddItemToArray(JsonLevel1, JsonLevel2);
- JsonLevel2 = cJSON_CreateString("zhang");
- cJSON_AddItemToArray(JsonLevel1, JsonLevel2);
- //-----------------
- JsonLevel1 = cJSON_CreateObject();
- if(NULL == JsonLevel1)
- {
- printf("MakeJsonIDPersonNameInfo: exec cJSON_CreateObject to get JsonLevel1 failed 2!");
- cJSON_Delete(root);
- return -1;
- }
- cJSON_AddStringToObject(JsonLevel1, "name", "zhou");
- cJSON_AddNumberToObject(JsonLevel1, "age", 30);
- cJSON_AddItemToObject(root, "personinfo", JsonLevel1);
- out=cJSON_Print(root);
- strncpy(pszJsonContent, out, iJsonLen - 1);
- pszJsonContent[iJsonLen - 1] = '\0';
- cJSON_Delete(root);
- free(out);
- return 0;
- }
7. 如果要實(shí)現(xiàn)如下JSON消息:
- {
- personinfo:{
- personname:[
- "zhou",
- "zhang"
- ],
- age:30
- }
- }
則編寫C代碼函數(shù)如下:
- int MakeJsonAgePersonNameInfo(char *pszJsonContent, int iJsonLen)
- {
- cJSON *root = NULL;
- cJSON *JsonLevel1 = NULL;
- cJSON *JsonLevel2 = NULL;
- cJSON *JsonLevel3 = NULL;
- char *out = NULL;
- // 判斷函數(shù)參數(shù)是否合法
- if (pszJsonContent == NULL)
- {
- printf("MakeJsonAgePersonNameInfo: pszJsonContent is NULL!");
- return -1;
- }
- root = cJSON_CreateObject();
- if (NULL == root)
- {
- printf("MakeJsonAgePersonNameInfo: exec cJSON_CreateObject to get root failed!");
- return -1;
- }
- JsonLevel1 = cJSON_CreateObject();
- if(NULL == JsonLevel1)
- {
- printf("MakeJsonAgePersonNameInfo: exec cJSON_CreateObject to get JsonLevel1 failed!");
- cJSON_Delete(root);
- return -1;
- }
- cJSON_AddItemToObject(root, "personinfo", JsonLevel1);
- //------------------
- JsonLevel2 = cJSON_CreateArray();
- if (NULL == JsonLevel2)
- {
- printf("MakeJsonAgePersonNameInfo: exec cJSON_CreateArray to get JsonLevel2 failed!");
- cJSON_Delete(root);
- return -1;
- }
- cJSON_AddItemToObject(JsonLevel1, "personname", JsonLevel2);
- JsonLevel3 = cJSON_CreateString("zhou");
- cJSON_AddItemToArray(JsonLevel2, JsonLevel3);
- JsonLevel3 = cJSON_CreateString("zhang");
- cJSON_AddItemToArray(JsonLevel2, JsonLevel3);
- //------------------
- cJSON_AddNumberToObject(JsonLevel1, "age", 30);
- out=cJSON_Print(root);
- strncpy(pszJsonContent, out, iJsonLen - 1);
- pszJsonContent[iJsonLen - 1] = '\0';
- cJSON_Delete(root);
- free(out);
- return 0;
- }
8. 如果要實(shí)現(xiàn)如下JSON消息:
- {
- personinfo:[
- {
- name:"zhou",
- age:30
- },
- {
- name:"zhang",
- age:41
- }
- ]
- }
則編寫C代碼函數(shù)如下:
- int MakeJsonPersonsInfo(char *pszJsonContent, int iJsonLen)
- {
- cJSON *root = NULL;
- cJSON *JsonLevel1 = NULL;
- cJSON *JsonLevel2 = NULL;
- char *out = NULL;
- // 判斷函數(shù)參數(shù)是否合法
- if (pszJsonContent == NULL)
- {
- printf("MakeJsonPersonsInfo: pszJsonContent is NULL!");
- return -1;
- }
- root = cJSON_CreateObject();
- if (NULL == root)
- {
- printf("MakeJsonPersonsInfo: exec cJSON_CreateObject to get root failed!");
- return -1;
- }
- JsonLevel1 = cJSON_CreateArray();
- if (NULL == JsonLevel1)
- {
- printf("MakeJsonPersonsInfo: exec cJSON_CreateArray to get JsonLevel1 failed!");
- cJSON_Delete(root);
- return -1;
- }
- cJSON_AddItemToObject(root, "personinfo", JsonLevel1);
- //---------------
- JsonLevel2 = cJSON_CreateObject();
- if(NULL == JsonLevel2)
- {
- printf("MakeJsonPersonsInfo: exec cJSON_CreateObject to get JsonLevel2 failed 1!");
- cJSON_Delete(root);
- return -1;
- }
- cJSON_AddItemToArray(JsonLevel1, JsonLevel2);
- cJSON_AddStringToObject(JsonLevel2, "name", "zhou");
- cJSON_AddNumberToObject(JsonLevel2, "age", 30);
- //---------------
- JsonLevel2 = cJSON_CreateObject();
- if(NULL == JsonLevel2)
- {
- printf("MakeJsonPersonsInfo: exec cJSON_CreateObject to get JsonLevel2 failed 2!");
- cJSON_Delete(root);
- return -1;
- }
- cJSON_AddItemToArray(JsonLevel1, JsonLevel2);
- cJSON_AddStringToObject(JsonLevel2, "name", "zhang");
- cJSON_AddNumberToObject(JsonLevel2, "age", 41);
- //---------------
- out=cJSON_Print(root);
- strncpy(pszJsonContent, out, iJsonLen - 1);
- pszJsonContent[iJsonLen - 1] = '\0';
- cJSON_Delete(root);
- free(out);
- return 0;
- }
總結(jié)
以上是常見JSON消息的C代碼實(shí)現(xiàn)方法,大家可以編寫測試代碼來看最終生成的JSON消息是否是我們描述的那樣。我編寫了一個(gè)完整的測試代碼,放到了GitHub上,歡迎下載閱讀:https://github.com/zhouzxi/TestJson。(本測試程序是運(yùn)行在Linux上的,大家可以使用這個(gè)命令進(jìn)行編譯:
- gcc -g -o TestJson TestJson.c cJSON.c -pthread -lc -lm)
【本文是51CTO專欄作者周兆熊的原創(chuàng)文章,作者微信公眾號(hào):周氏邏輯(logiczhou)】