JSON简介及C代码展示JSON消息示例
创始人
2025-02-12 20:11:36
0次

在互联网软件前端与后台进行消息交互的过程中,需要有一种标准的数据交换格式供前后端采用。在众多的数据交换格式中,JSON(JavaScript Object Notation,JS 对象标记)是应用得比较广泛的,它采用完全独立于编程语言的文本格式来存储和表示数据。JSON的层次结构简洁、清晰,易于阅读和编写,同时也易于机器解析和生成,这有效地提升了网络传输效率。

[[192401]]

本文首先对JSON进行简单的介绍,然后用具体的C代码示范了各类JSON消息的构造方法。

JSON简介

JSON 的语法规则可以用下面的四句话来概括:

  • ***,对象表示为键值对。
  • 第二,数据由逗号分隔。
  • 第三,花括号保存对象。
  • 第四,方括号保存数组。

具体而言,键值对组合中的键名写在前面并用双引号包裹,键值使用冒号分隔,冒号后面紧接着值,如:”name”: “zhou”;数组是用方括号包裹起来的,如:[“zhou”, “zhang”]。

JSON消息示例

本部分用实际的C代码来示范了各类常用的JSON消息的构造方法。在编写代码之前,要到https://sourceforge.net/projects/cjson/上去下载C语言版的JSON封装API。

在JSON的API中,我们常用到的有如下几个函数:

1)cJSON_CreateObject():创建JSON对象。

2)cJSON_Delete(cJSON *c):删除一个JSON结构。

3)cJSON_AddStringToObject(object,name,s):将一个字符串添加到对象中。

4)cJSON_AddNumberToObject(object,name,n):将一个整数添加到对象中。

5)cJSON_Print(cJSON *item):将JSON消息以文本消息的样式输出。

6)cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item):将一个数据(通常为对象)添加到一个对象中。

7)cJSON_CreateString(const char *string):生成字符串数据。

8)cJSON_AddItemToArray(cJSON *array, cJSON *item):将一个数据添加到一个数组中。

9)cJSON_CreateArray():创建JSON数组。

下面,我们开始编写C代码来生成JSON消息。

1. 如果要实现如下JSON消息:

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

则编写C代码函数如下:

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

2. 如果要实现如下JSON消息:

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

则编写C代码函数如下:

  1. int MakeJsonPersonInfo(char *pszJsonContent, int iJsonLen) 
  2. { 
  3.     cJSON *root        = NULL; 
  4.     cJSON *JsonLevel1  = NULL; 
  5.     char  *out         = NULL; 
  6.  
  7.     // 判断函数参数是否合法 
  8.     if (pszJsonContent == NULL) 
  9.     { 
  10.         printf("MakeJsonPersonInfo: pszJsonContent is NULL!"); 
  11.  
  12.         return -1; 
  13.     } 
  14.  
  15.     root = cJSON_CreateObject(); 
  16.     if(NULL == root) 
  17.     { 
  18.         printf("MakeJsonPersonInfo: exec cJSON_CreateObject to get root failed!"); 
  19.  
  20.         return -1; 
  21.     } 
  22.  
  23.     JsonLevel1 = cJSON_CreateObject(); 
  24.     if(NULL == JsonLevel1) 
  25.     { 
  26.         printf("MakeJsonPersonInfo: exec cJSON_CreateObject to get JsonLevel1 failed!"); 
  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, "personinfo", JsonLevel1); 
  37.  
  38.     out=cJSON_Print(root); 
  39.     strncpy(pszJsonContent, out, iJsonLen - 1); 
  40.     pszJsonContent[iJsonLen - 1] = '\0'; 
  41.  
  42.     cJSON_Delete(root); 
  43.     free(out); 
  44.  
  45.     return 0; 
  46. } 

3. 如果要实现如下JSON消息:

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

 则编写C代码函数如下:

  1. int MakeJsonTwoPersonInfo(char *pszJsonContent, int iJsonLen) 
  2. { 
  3.     cJSON *root        = NULL; 
  4.     cJSON *JsonLevel1  = NULL; 
  5.     char  *out         = NULL; 
  6.  
  7.     // 判断函数参数是否合法 
  8.     if (pszJsonContent == NULL) 
  9.     { 
  10.         printf("MakeJsonTwoPersonInfo: pszJsonContent is NULL!"); 
  11.  
  12.         return -1; 
  13.     } 
  14.  
  15.     root = cJSON_CreateObject(); 
  16.     if(NULL == root) 
  17.     { 
  18.         printf("MakeJsonTwoPersonInfo: exec cJSON_CreateObject to get root failed!"); 
  19.  
  20.         return -1; 
  21.     } 
  22.  
  23.     //--------------- 
  24.     JsonLevel1 = cJSON_CreateObject(); 
  25.     if(NULL == JsonLevel1) 
  26.     { 
  27.         printf("MakeJsonTwoPersonInfo: exec cJSON_CreateObject to get JsonLevel1 failed 1!"); 
  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, "personinfo1", JsonLevel1); 
  38.  
  39.     //--------------- 
  40.     JsonLevel1 = cJSON_CreateObject(); 
  41.     if(NULL == JsonLevel1) 
  42.     { 
  43.         printf("MakeJsonTwoPersonInfo: exec cJSON_CreateObject to get JsonLevel1 failed 2!"); 
  44.  
  45.         cJSON_Delete(root); 
  46.         return -1; 
  47.     }    
  48.  
  49.     cJSON_AddStringToObject(JsonLevel1, "name", "zhang"); 
  50.  
  51.     cJSON_AddNumberToObject(JsonLevel1, "age", 40); 
  52.  
  53.     cJSON_AddItemToObject(root, "personinfo2", JsonLevel1); 
  54.  
  55.     out=cJSON_Print(root); 
  56.     strncpy(pszJsonContent, out, iJsonLen - 1); 
  57.     pszJsonContent[iJsonLen - 1] = '\0'; 
  58.  
  59.     cJSON_Delete(root); 
  60.     free(out); 
  61.  
  62.     return 0; 
  63. } 

4. 如果要实现如下JSON消息:

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

则编写C代码函数如下:

  1. int MakeJsonIDPersonInfo(char *pszJsonContent, int iJsonLen) 
  2. { 
  3.     cJSON *root        = NULL; 
  4.     cJSON *JsonLevel1  = NULL; 
  5.     char  *out         = NULL; 
  6.  
  7.     // 判断函数参数是否合法 
  8.     if (pszJsonContent == NULL) 
  9.     { 
  10.         printf("MakeJsonIDPersonInfo: pszJsonContent is NULL!"); 
  11.  
  12.         return -1; 
  13.     } 
  14.  
  15.     root = cJSON_CreateObject(); 
  16.     if(NULL == root) 
  17.     { 
  18.         printf("MakeJsonIDPersonInfo: exec cJSON_CreateObject to get root failed!"); 
  19.  
  20.         return -1; 
  21.     } 
  22.  
  23.     cJSON_AddStringToObject(root, "id", "123456"); 
  24.  
  25.     JsonLevel1 = cJSON_CreateObject(); 
  26.     if(NULL == JsonLevel1) 
  27.     { 
  28.         printf("MakeJsonIDPersonInfo: exec cJSON_CreateObject to get JsonLevel1 failed!"); 
  29.  
  30.         cJSON_Delete(root); 
  31.         return -1; 
  32.     }    
  33.  
  34.     cJSON_AddStringToObject(JsonLevel1, "name", "zhou"); 
  35.  
  36.     cJSON_AddNumberToObject(JsonLevel1, "age", 30); 
  37.  
  38.     cJSON_AddItemToObject(root, "personinfo", JsonLevel1); 
  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; 
  48. } 

5. 如果要实现如下JSON消息:

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

则编写C代码函数如下:

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

6. 如果要实现如下JSON消息:

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

 则编写C代码函数如下:

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

7. 如果要实现如下JSON消息:

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

则编写C代码函数如下:

  1. int MakeJsonAgePersonNameInfo(char *pszJsonContent, int iJsonLen) 
  2. { 
  3.     cJSON *root        = NULL; 
  4.     cJSON *JsonLevel1  = NULL; 
  5.     cJSON *JsonLevel2  = NULL; 
  6.     cJSON *JsonLevel3  = NULL; 
  7.     char  *out         = NULL; 
  8.  
  9.     // 判断函数参数是否合法 
  10.     if (pszJsonContent == NULL) 
  11.     { 
  12.         printf("MakeJsonAgePersonNameInfo: pszJsonContent is NULL!"); 
  13.  
  14.         return -1; 
  15.     } 
  16.  
  17.     root = cJSON_CreateObject(); 
  18.     if (NULL == root) 
  19.     { 
  20.         printf("MakeJsonAgePersonNameInfo: exec cJSON_CreateObject to get root failed!"); 
  21.  
  22.         return -1; 
  23.     } 
  24.  
  25.     JsonLevel1 = cJSON_CreateObject(); 
  26.     if(NULL == JsonLevel1) 
  27.     { 
  28.         printf("MakeJsonAgePersonNameInfo: exec cJSON_CreateObject to get JsonLevel1 failed!"); 
  29.  
  30.         cJSON_Delete(root); 
  31.         return -1; 
  32.     }    
  33.  
  34.     cJSON_AddItemToObject(root, "personinfo", JsonLevel1); 
  35.  
  36.     //------------------ 
  37.     JsonLevel2 = cJSON_CreateArray(); 
  38.     if (NULL == JsonLevel2) 
  39.     { 
  40.         printf("MakeJsonAgePersonNameInfo: exec cJSON_CreateArray to get JsonLevel2 failed!"); 
  41.  
  42.         cJSON_Delete(root); 
  43.         return -1; 
  44.     } 
  45.  
  46.     cJSON_AddItemToObject(JsonLevel1, "personname", JsonLevel2); 
  47.  
  48.     JsonLevel3 = cJSON_CreateString("zhou"); 
  49.     cJSON_AddItemToArray(JsonLevel2, JsonLevel3); 
  50.  
  51.     JsonLevel3 = cJSON_CreateString("zhang"); 
  52.     cJSON_AddItemToArray(JsonLevel2, JsonLevel3); 
  53.  
  54.     //------------------ 
  55.     cJSON_AddNumberToObject(JsonLevel1, "age", 30); 
  56.  
  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; 
  66. } 

8. 如果要实现如下JSON消息:

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

则编写C代码函数如下:

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

总结 

以上是常见JSON消息的C代码实现方法,大家可以编写测试代码来看最终生成的JSON消息是否是我们描述的那样。我编写了一个完整的测试代码,放到了GitHub上,欢迎下载阅读:https://github.com/zhouzxi/TestJson。(本测试程序是运行在Linux上的,大家可以使用这个命令进行编译:

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

【本文是51CTO专栏作者周兆熊的原创文章,作者微信公众号:周氏逻辑(logiczhou)】

戳这里,看该作者更多好文

相关内容

热门资讯

PHP新手之PHP入门 PHP是一种易于学习和使用的服务器端脚本语言。只需要很少的编程知识你就能使用PHP建立一个真正交互的...
网络中立的未来 网络中立性是什... 《牛津词典》中对“网络中立”的解释是“电信运营商应秉持的一种原则,即不考虑来源地提供所有内容和应用的...
各种千兆交换机的数据接口类型详... 千兆交换机有很多值得学习的地方,这里我们主要介绍各种千兆交换机的数据接口类型,作为局域网的主要连接设...
粉嫩如何诠释霸道 东芝M805... “霸道粉”是个什么玩意东芝M805拿过来的时候,笔者扑哧笑了,不是笑这款笔记本,而是笑这款产品的颜色...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
什么是大数据安全 什么是大数据... 在《为什么需要大数据安全分析》一文中,我们已经阐述了一个重要观点,即:安全要素信息呈现出大数据的特征...
只需三步,让你掌握阿尔法路由器... 请参照以下阿尔法路由器设置方法,通过浏览器进入及设置宽带路由器。经过这三个步骤的设置,基本上能够熟悉...
如何更省钱? 近期特价热门硒鼓... 如今激光打印设备已经成为商务或企业的绝对主力办公设备,但采购者往往忽视了激光打印设备所用的耗材,相对...
全面诠释网络负载均衡 负载均衡的出现大大缓解了服务器的压力,更是有效的利用了资源,提高了效率。那么我们现在来说一下网络负载...
如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...