系统运维

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

时间:2010-12-5 17:23:32  作者:IT科技类资讯   来源:域名  查看:  评论:0
内容摘要:在互联网软件前端与后台进行消息交互的过程中,需要有一种标准的数据交换格式供前后端采用。在众多的数据交换格式中,JSON(JavaScript Object Notation,JS 对象标记)是应用得比

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

本文首先对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消息:

{      name:"zhou",     age:30 } 

则编写C代码函数如下:

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

{      personinfo:{          name:"zhou",         age:30     } } 

则编写C代码函数如下:

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

{      personinfo1:{          name:"zhou",         age:30     },     personinfo2:{          name:"zhang",         age:41     } } 

 则编写C代码函数如下:

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

{      id:"123456",     personinfo:{          name:"zhou",         age:30     } } 

则编写C代码函数如下:

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

{      personname:[         "zhou",         "zhang"     ] } 

则编写C代码函数如下:

int MakeJsonPersonNameInfo(char *pszJsonContent, int iJsonLen) {      cJSON *root        = NULL;     cJSON *JsonLevel1  = NULL;     cJSON *JsonLevel2  = NULL;     char  *out         = NULL;     // 判断函数参数是否合法     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. 如果要实现如下JSON消息:

{      id:"123456",     personname:[         "zhou",         "zhang"     ],     personinfo:{          phonenumber:"15696192591",         age:30     } } 

 则编写C代码函数如下:

int MakeJsonIDPersonNameInfo(char *pszJsonContent, int iJsonLen) {      cJSON *root        = NULL;     cJSON *JsonLevel1  = NULL;     cJSON *JsonLevel2  = NULL;     char  *out         = NULL;     // 判断函数参数是否合法     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. 如果要实现如下JSON消息:

{      personinfo:{          personname:[         "zhou",         "zhang"         ],         age:30     } } 

则编写C代码函数如下:

int MakeJsonAgePersonNameInfo(char *pszJsonContent, int iJsonLen) {      cJSON *root        = NULL;     cJSON *JsonLevel1  = NULL;     cJSON *JsonLevel2  = NULL;     cJSON *JsonLevel3  = NULL;     char  *out         = NULL;     // 判断函数参数是否合法     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. 如果要实现如下JSON消息:

{      personinfo:[     {          name:"zhou",         age:30     },     {          name:"zhang",         age:41     }     ] } 

则编写C代码函数如下:

int MakeJsonPersonsInfo(char *pszJsonContent, int iJsonLen) {      cJSON *root        = NULL;     cJSON *JsonLevel1  = NULL;     cJSON *JsonLevel2  = NULL;     char  *out         = NULL;     // 判断函数参数是否合法     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; } 

总结 

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

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

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

戳这里,网站模板看该作者更多好文

copyright © 2025 powered by 益强资讯全景  滇ICP备2023006006号-31sitemap