JSON简介及具体的JSON消息示例

栏目: C · 发布时间: 6年前

内容简介:JSON简介及具体的JSON消息示例

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


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Convergence Culture

Convergence Culture

Henry Jenkins / NYU Press / 2006-08-01 / USD 30.00

"Convergence Culture" maps a new territory: where old and new media intersect, where grassroots and corporate media collide, where the power of the media producer, and the power of the consumer intera......一起来看看 《Convergence Culture》 这本书的介绍吧!

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具