在互联网软件的前后端消息交互过程中,需要有一种标准的数据交换格式供前后端采用。在众多的数据交换格式中,JSON(JavaScriptObjectNotation,JS对象标签)应用最为广泛。它使用完全独立于编程语言的文本格式来存储和表示数据。JSON的层次结构简洁明了,易于读写,易于机器解析和生成,有效提高了网络传输的效率。本文先简单介绍一下JSON,然后结合具体的C代码演示各种JSON报文的构造方法。JSON简介JSON的语法规则可以概括为以下四句话:***,对象被表示为键值对。其次,数据以逗号分隔。第三,花括号持有对象。第四,方括号保存数组。具体来说,键值对组合中的键名写在前面并用双引号括起来,键值之间用冒号隔开,值后面跟冒号,如:"name":"zhou";数组用方括号括起来,例如:["zhou","zhang"]。JSON消息示例这部分使用实际的C代码来演示各种常用的JSON消息的构造方法。在写代码之前,先到https://sourceforge.net/projects/cjson/下载JSON包API的C语言版本。在JSONAPI中,我们常用到的函数如下: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,constchar*string,cJSON*item):将一段数据(通常是一个对象)添加到一个对象中。7)cJSON_CreateString(constchar*string):生成字符串数据。8)cJSON_AddItemToArray(cJSON*array,cJSON*item):添加一条数据到一个数组中。9)cJSON_CreateArray():创建一个JSON数组。接下来,我们开始编写C代码来生成JSON消息。1、如果要实现如下JSON消息:{name:"zhou",age:30},那么编写C代码函数如下:intMakeJsonNameAge(char*pszJsonContent,intiJsonLen){cJSON*root=NULL;char*out=NULL;//判断函数参数是否合法if(pszJsonContent==NULL){printf("MakeJsonNameAge:pszJsonContentisNULL!");return-1;}root=cJSON_CreateObject();if(NULL==root){printf("MakeJsonNameAge:execcJSON_CreateObjecttogetrootfailed!");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);return0;}2.如果要实现如下JSON消息:{personinfo:{name:"zhou",age:30}}然后编写C代码函数如下:intMakeJsonPersonInfo(char*pszJsonContent,intiJsonLen){cJSON*root=NULL;cJSON*JsonLevel1=NULL;char*out=NULL;//判断是否函数参数合法if(pszJsonContent==NULL){printf("MakeJsonPersonInfo:pszJsonContentisNULL!");return-1;}root=cJSON_CreateObject();if(NULL==root){printf("MakeJsonPersonInfo:execcJSON_CreateObjecttogetrootFailed!");return-1;}JsonLevel1=cJSON_CreateObject();if(NULL==JsonLevel1){printf("MakeJsonPersonInfo:execcJSON_CreateObjecttogetJsonLevel1failed!");cJSON_Delete(root);return-1;}cJSON_AddStringToObject,JsonLevel"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);return0;}3.如果要实现如下JSON消息:{personinfo1:{name:"zhou",age:30},personinfo2:{name:"zhang",age:41}}然后编写C代码函数如下:intMakeJsonTwoPersonInfo(char*pszJsonContent,intiJsonLen){cJSON*root=NULL;cJSON*JsonLevel1=NULL;char*out=NULL;//判断函数参数是否合法if(pszJsonContent==NULL){printf("MakeJsonTwoPersonInfo:pszJsonContentisNULL!");return-1;}root=cJSON_CreateObject();if(NULL==root){printf("MakeJsonTwoPersonInfo:execcJSON_CreateObjecttogetrootfailed!");return-1;}//----------------JsonLevel1=cJSON_CreateObject();if(NULL==JsonLevel1){printf("MakeJsonTwoPersonInfo:execcJSON_CreateObjecttogetJsonLevel1failed1!");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:execcJSON_CreateObjecttogetJsonLevel1failed2!");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);return0;}4.如果要实现如下JSON消输入:{id:"123456",personinfo:{name:"zhou",age:30}}创建C属性:intMakeJsonIDPersonInfo(char*pszJsonContent,intiJsonLen){cJSON*root=NULL;cJSON*JsonLevel1=NULL;char*out=NULL;//return-1;}root=cJSON_CreateObject();if(NULL==root);;char*out=NULL;{printf("MakeJsonIDPersonInfo:execcJSON_CreateObjecttogetrootfailed!");return-1;}cJSON_AddStringToObject(root,"id","123456");JsonLevel1=cJson_CreateObject();if(NULL==JsonLevel1){printf("MakeJsonIDPersonInfo:execc!");cJSON_Delete(root);return-1;}cJSON_AddStringToObject(JsonLevel1,"name","zhou");cJSON_AddNumberToObject(JsonLevel1,"age",30);cJSON_Print(根);strncpy(pszJsonContent,out,iJsonLen-1);ree(out);return0;}5.如果要实现如下JSON消息:{personname:["zhou","zhang"]},编写C代码函数如下:intMakeJsonPersonNameInfo(char*pszJsonContent,intiJsonLen){cJSON*root=NULL;cJSON*JsonLevel1=NULL;cJSON*JsonLevel2=NULL;char*out=NULL;//判断函数参数是否合法if(pszJsonContent==NULL){printf("MakeJsonPersonNameInfo:pszJsonContentisNULL!");return-1;}root=cJSON_CreateObject();if(NULL==root){printf("MakeJsonPersonNameInfo:execcJSON_CreateObjecttogetrootfailed!");return-1;}JsonLevel1=cJSON_CreateArray();if(NULL==JsonLevel1){printf("MakeJsonPersonNameInfo:execcJSON_CreateArraytogetJsonLevel1failed!");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);return0;}6.如果要实现如下JSON消息:{id:"123456",personname:["zhou","zhang"],personinfo:{phonenumber:"15696192591",age:30}}然后编写C代码函数如下:intMakeJsonIDPersonNameInfo(char*pszJsonContent,intiJsonLen){cJSON*root=NULL;cJSON*JsonLevel1=NULL;cJSON*JsonLevel2=NULL;char*out=NULL;//判断函数参数是否合法if(pszJsonContent==NULL){printf("MakeJsonIDPersonNameInfo:pszJsonContentisNULL!");return-1;}root=cJSON_CreateObject();if(NULL==root){printf("MakeJsonIDPersonNameInfo:execcJSON_CreateObjecttogetrootfailed!");return-1;}cJSON_AddStringToObject(root,"id","123456");JsonLevel1=cJSON_CreateArray();if(NULL==JsonLevel1){printf("MakeJsonIDPersonNameInfo:execcJSON_CreateArray1togetedJsonLevel1f");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:execcJSON_CreateObjecttogetJsonLevel1failed2!");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);返回0;}7。如果要实现如下JSON消息:{personinfo:{personname:["zhou","zhang"],age:30}},编写C代码函数如下:intMakeJsonAgePersonNameInfo(char*pszJsonContent,intiJsonLen){cJSON*root=NULL;cJSON*JsonLevel1=NULL;cJSON*JsonLevel2=NULL;cJSON*JsonLevel3=NULL;char*out=NULL;//判断函数参数是否合法if(pszJsonContent==NULL){printf("MakeJsonAgePersonNameInfo:pszJsonContentsNULL!");return-1;}root=cJson_CreateObject();if(NULL==root){printf("MakeJsonAgePersonNameInfo:execcJson_CreateObjecttogetrootfailed!");return-1;}JsonLevel1=cJson_CreateObject();if(NULL==JsonLevel1){printf("MakeJsonAgePersonNameInfo:execcJSON_CreateObjecttogetJsonLevel1失败!");cJSON_Delete(root);return-1;}cJSON_AddItemToObject(root,"personinfo",JsonLevel1);-----JsonLevel2=cJSON_CreateArray();if(NULL==JsonLevel2){printf("MakeJsonAgePersonNameInfo:execcJSON_CreateArraytogetJsonLevel2failed!");cJSON_Delete(root);return-1;}cJSON_AddItemToObject(JsonLevel1,"personname",JsonLevel2);JsonLevel3=cJSON_CreateString("zhou");cJSON_AddItemToArray(JsonLevel2,JsonLevel2);JsonLevel3=cJSON_CreateString("zhang");--cJSON_AddNumberToObject(JsonLevel1,"age",30);out=cJSON_Print(root);strncpy(pszJsonContent,out,iJsonLen-1);pszJsonContent[iJsonLen-1]='\0';cJSON_Delete(root);free(out);return0;}8.如果要实现如下JSON消息:{personinfo:[{name:"zhou",age:30},{name:"zhang",age:41}]}然后编写C代码函数如下:intMakeJsonPersonsInfo(char*pszJsonContent,intiJsonLen){cJSON*root=NULL;cJSON*JsonLevel1=NULL;cJSON*JsonLevel2=NULL;char*out=NULL;//判断函数参数是否合法if(pszJsonContent==NULL){printf("MakeJsonPersonsInfo:pszJsonContentisNULL!");return-1;}root=cJSON_CreateObject();if(NULL==root){printf("MakeJsonPersonsInfo:execcJSON_CreateObjecttogetJsonLevel1failed!");return-1;}JsonLevel1=cJSON_CreateArray();if(NULL==JsonLevel1){printf("MakeJsonPersonsInfo:execcJSON_CreateArraytogetJsonLevel1failed!");cJSON_Delete(root);return-1;}cJSON_AddItemToObject(root,"personinfo",JsonLevel1);//------------JsonLevel2=cJSON_CreateObject();if(NULL==JsonLevel2){printf("MakeJsonPersonsInfo:execcJSON_CreateObjecttogetJsonLevel2failed1!");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:execcJSON_CreateObjecttogetJsonLevel2failed2!");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);return0;}总结以上是常见的JSON消息的C代码实现方法,可以这样写测试代码为了查看最终生成的JSON消息是否是我们所描述的,我写了一个完整的测试代码并将其放在GitHub上。欢迎下载阅读:https://github.com/zhouzxi/TestJson。(本测试程序运行在Linux上,可以用这个命令编译:gcc-g-oTestJsonTestJson.ccJSON.c-pthread-lc-lm):周氏逻辑(logiczhou)】点此阅读更多本作者好文
