JSON简介及C代码展示JSON消息示例

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

在互联网软件前端与后台进行消息交互的过程中,需要有一种标准的数据交换格式供前后端采用。在众多的数据交换格式中,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.     name:"zhou", 
  2.     age:30 

则编写C代码函数如下:

  1. int MakeJsonNameAge(char *pszJsonContent, int iJsonLen) 
  2.     cJSON *root = NULL
  3.     char  *out  = NULL
  4.  
  5.     // 判断函数参数是否合法 
  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. 如果要实现如下JSON消息:

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

则编写C代码函数如下:

  1. int MakeJsonPersonInfo(char *pszJsonContent, int iJsonLen) 
  2.     cJSON *root        = NULL
  3.     cJSON *JsonLevel1  = NULL
  4.     char  *out         = NULL
  5.  
  6.     // 判断函数参数是否合法 
  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. 如果要实现如下JSON消息:

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

 则编写C代码函数如下:

  1. int MakeJsonTwoPersonInfo(char *pszJsonContent, int iJsonLen) 
  2.     cJSON *root        = NULL
  3.     cJSON *JsonLevel1  = NULL
  4.     char  *out         = NULL
  5.  
  6.     // 判断函数参数是否合法 
  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. 如果要实现如下JSON消息:

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

则编写C代码函数如下:

  1. int MakeJsonIDPersonInfo(char *pszJsonContent, int iJsonLen) 
  2.     cJSON *root        = NULL
  3.     cJSON *JsonLevel1  = NULL
  4.     char  *out         = NULL
  5.  
  6.     // 判断函数参数是否合法 
  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. 如果要实现如下JSON消息:

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

则编写C代码函数如下:

  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.     // 判断函数参数是否合法 
  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. 如果要实现如下JSON消息:

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

 则编写C代码函数如下:

  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.     // 判断函数参数是否合法 
  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. 如果要实现如下JSON消息:

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

则编写C代码函数如下:

  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.     // 判断函数参数是否合法 
  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. 如果要实现如下JSON消息:

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

则编写C代码函数如下:

  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.     // 判断函数参数是否合法 
  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; 

总结 

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

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

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

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

责任编辑:赵宁宁 来源: 51CTO专栏
相关推荐

2020-07-20 07:56:28

JavaScript开发技术

2019-07-16 08:38:34

JavaJson库数据

2011-07-01 15:28:26

PhoneGap代码示例

2015-07-02 10:37:32

C#Json字符串类代码

2010-01-06 17:06:05

Json格式

2016-12-20 11:12:11

C代码自测开发

2009-08-27 15:53:30

C#中using wo

2023-11-12 11:56:28

Json格式弊端

2014-07-10 10:09:11

JSON数据行转列

2016-12-12 12:37:45

结构C代码赋值

2010-02-05 10:23:09

C++基本函数

2009-09-16 16:32:20

JavaScript静

2009-07-03 17:44:06

JSP介绍

2009-09-01 16:49:56

C#文件上传下载

2010-01-05 13:29:50

JSON对象

2010-01-05 16:48:16

JSON 字符串

2009-08-17 17:36:08

C# 枚举

2010-01-05 13:54:32

Jquery Json

2010-02-03 10:05:48

C++ enum枚举

2011-02-23 15:49:49

点赞
收藏

51CTO技术栈公众号