使用cjson进行对象的嵌套封装

简介:

共分两个部分,1)创建json、2)解析json

1)创建嵌套json的代码

复制代码
char * makeJson()
{
    cJSON * pRoot = NULL;
    cJSON * pSub_1 = NULL;
    cJSON * pSub_2 = NULL;

    if((pRoot = cJSON_CreateObject()) == NULL)
    {
        return NULL;
    }
    if((pSub_1 = cJSON_CreateObject()) == NULL)
    {
        return NULL;
    }
    if((pSub_2 = cJSON_CreateObject()) == NULL)
    {
        return NULL;
    }
    cJSON_AddStringToObject(pSub_2, "cStr", "ccccccc");

    cJSON_AddStringToObject(pSub_1, "bStr", "bbbbbbb");
    cJSON_AddItemToObject(pSub_1, "subobject_2", pSub_2);

    cJSON_AddStringToObject(pRoot, "aStr", "aaaaaaa");
    cJSON_AddItemToObject(pRoot, "subobject_1", pSub_1);
    cJSON_AddStringToObject(pRoot, "xStr", "xxxxxxx");

    //cJSON_PrintUnformatted : make json string for Unformatted
    //char * pJson = cJSON_PrintUnformatted(pRoot);

    char * pJson = cJSON_Print(pRoot);
    if(NULL == pJson)
    {
        cJSON_Delete(pRoot);
        return NULL;
    }
    return pJson;
}
复制代码

2)解析json的代码

复制代码
int parseJson(const char * pJson)
{
    if(NULL == pJson)
    {
        return 1;
    }
    cJSON * pRoot = cJSON_Parse(pJson);
    if(NULL == pRoot)
    {
        return 2;
    }
    cJSON * pSub_1 = cJSON_GetObjectItem(pRoot, "aStr");
    if(NULL == pSub_1)
    {
        cJSON_Delete(pRoot);
        return 3;
    }
    printf("get aStr : [%s]\n", pSub_1->valuestring);
    pSub_1 = cJSON_GetObjectItem(pRoot, "xStr");
    if(NULL == pSub_1)
    {
        cJSON_Delete(pRoot);
        return 4;
    }
    printf("get xStr : [%s]\n", pSub_1->valuestring);
    pSub_1 = cJSON_GetObjectItem(pRoot, "subobject_1");
    if(NULL == pSub_1)
    {
        cJSON_Delete(pRoot);
        return 5;
    }
    printf("get Sub Obj 1\n");
    cJSON * pSub_2 = cJSON_GetObjectItem(pSub_1, "bStr");
    if(NULL == pSub_2)
    {
        cJSON_Delete(pRoot);
        return 6;
    }
    printf("get bStr : [%s]\n", pSub_2->valuestring);
    pSub_2 = cJSON_GetObjectItem(pSub_1, "subobject_2");
    if(NULL == pSub_2)
    {
        cJSON_Delete(pRoot);
        return 7;
    }
    printf("get Obj 2\n");
    cJSON * pStr = cJSON_GetObjectItem(pSub_2, "cStr");
    if(NULL == pStr)
    {
        cJSON_Delete(pRoot);
        return 8;
    }
    printf("get cStr : [%s]\n", pStr->valuestring);

    cJSON_Delete(pRoot);
    return 0;
}
复制代码

3)主函数

复制代码
int main()
{
    char * pJson = makeJson();
    printf("JSON:\n%s\n", pJson);
    parseJson(pJson);
    free(pJson);

    return 0;
}
复制代码

完整的代码请打开下面的代码或到百度网盘下载 http://pan.baidu.com/s/1pJ7KZSR

复制代码
#include <stdio.h>
#include "cJSON.h"

char * makeJson()
{
    cJSON * pRoot = NULL;
    cJSON * pSub_1 = NULL;
    cJSON * pSub_2 = NULL;

    if((pRoot = cJSON_CreateObject()) == NULL)
    {
        return NULL;
    }
    if((pSub_1 = cJSON_CreateObject()) == NULL)
    {
        return NULL;
    }
    if((pSub_2 = cJSON_CreateObject()) == NULL)
    {
        return NULL;
    }
    cJSON_AddStringToObject(pSub_2, "cStr", "ccccccc");

    cJSON_AddStringToObject(pSub_1, "bStr", "bbbbbbb");
    cJSON_AddItemToObject(pSub_1, "subobject_2", pSub_2);

    cJSON_AddStringToObject(pRoot, "aStr", "aaaaaaa");
    cJSON_AddItemToObject(pRoot, "subobject_1", pSub_1);
    cJSON_AddStringToObject(pRoot, "xStr", "xxxxxxx");

    //cJSON_PrintUnformatted : make json string for Unformatted
    //char * pJson = cJSON_PrintUnformatted(pRoot);

    char * pJson = cJSON_Print(pRoot);
    if(NULL == pJson)
    {
        cJSON_Delete(pRoot);
        return NULL;
    }
    return pJson;
}

int parseJson(const char * pJson)
{
    if(NULL == pJson)
    {
        return 1;
    }
    cJSON * pRoot = cJSON_Parse(pJson);
    if(NULL == pRoot)
    {
        return 2;
    }
    cJSON * pSub_1 = cJSON_GetObjectItem(pRoot, "aStr");
    if(NULL == pSub_1)
    {
        cJSON_Delete(pRoot);
        return 3;
    }
    printf("get aStr : [%s]\n", pSub_1->valuestring);
    pSub_1 = cJSON_GetObjectItem(pRoot, "xStr");
    if(NULL == pSub_1)
    {
        cJSON_Delete(pRoot);
        return 4;
    }
    printf("get xStr : [%s]\n", pSub_1->valuestring);
    pSub_1 = cJSON_GetObjectItem(pRoot, "subobject_1");
    if(NULL == pSub_1)
    {
        cJSON_Delete(pRoot);
        return 5;
    }
    printf("get Sub Obj 1\n");
    cJSON * pSub_2 = cJSON_GetObjectItem(pSub_1, "bStr");
    if(NULL == pSub_2)
    {
        cJSON_Delete(pRoot);
        return 6;
    }
    printf("get bStr : [%s]\n", pSub_2->valuestring);
    pSub_2 = cJSON_GetObjectItem(pSub_1, "subobject_2");
    if(NULL == pSub_2)
    {
        cJSON_Delete(pRoot);
        return 7;
    }
    printf("get Obj 2\n");
    cJSON * pStr = cJSON_GetObjectItem(pSub_2, "cStr");
    if(NULL == pStr)
    {
        cJSON_Delete(pRoot);
        return 8;
    }
    printf("get cStr : [%s]\n", pStr->valuestring);

    cJSON_Delete(pRoot);
    return 0;
}

int main()
{
    char * pJson = makeJson();
    printf("JSON:\n%s\n", pJson);
    parseJson(pJson);
    free(pJson);

    return 0;
}
复制代码

编译

$ gcc -o nestcjson nestcjson.c cjson.c -lm

:编译时链接的库 -lm 是数学库,不加此库时 gcc 返回错误,错误代码如下

复制代码
$ gcc -o nestcjson nestcjson.c cjson.c 
/tmp/ccugp95L.o: In function `parse_number':
cjson.c:(.text+0x402): undefined reference to `pow'
/tmp/ccugp95L.o: In function `print_number':
cjson.c:(.text+0x512): undefined reference to `floor'
collect2: ld 返回 1
复制代码

运行

复制代码
$ ./nestcjson 
JSON:
{
    "aStr":    "aaaaaaa",
    "subobject_1":    {
        "bStr":    "bbbbbbb",
        "subobject_2":    {
            "cStr":    "ccccccc"
        }
    },
    "xStr":    "xxxxxxx"
}
get aStr : [aaaaaaa]
get xStr : [xxxxxxx]
get Sub Obj 1
get bStr : [bbbbbbb]
get Obj 2
get cStr : [ccccccc]
复制代码

 


本文转自郝峰波博客园博客,原文链接:http://www.cnblogs.com/fengbohello/p/4363007.html,如需转载请自行联系原作者


相关文章
|
6月前
|
数据安全/隐私保护 Python
Python中装饰器、回调函数、闭包、派生的区别与应用详解
Python中装饰器、回调函数、闭包、派生的区别与应用详解
67 0
|
6月前
|
编译器 Python
python之局部变量和全局变量的定义,两者之间的区别和使用方法,global和nonlocal的定义和使用方法,可变与不可变类型的定义和示例
python之局部变量和全局变量的定义,两者之间的区别和使用方法,global和nonlocal的定义和使用方法,可变与不可变类型的定义和示例
|
29天前
|
存储 JavaScript 前端开发
对象字面量和对象的封装(结合柯里化)
对象字面量和对象的封装(结合柯里化)
30 0
|
1月前
|
存储 程序员 Python
Python函数定义与调用详解
Python中的函数是可重用代码块,用于接收参数、执行操作并可能返回输出。通过`def`定义函数,如`def greet(name): print(f&quot;Hello, {name}!&quot;)`。函数可接受任意数量的参数,包括默认值。调用函数时提供参数,如`greet(&quot;Alice&quot;)`。可变参数通过星号(*)和双星号(**)实现。函数有助于代码模块化、理解和维护。掌握函数是Python编程基础。
C4.
|
1月前
|
Python
Phython函数封装
Phython函数封装
C4.
6 0
C4.
|
1月前
|
Serverless C语言
C语言函数的嵌套调用
C语言函数的嵌套调用
C4.
28 0
|
4月前
|
JavaScript 前端开发
函数:函数是JS的基本组成单元,用于封装代码块以实现特定功能。理解函数的定义、调用和参数传递方式非常重要。
函数:函数是JS的基本组成单元,用于封装代码块以实现特定功能。理解函数的定义、调用和参数传递方式非常重要。
29 0
|
4月前
|
JavaScript 前端开发
js函数:函数是JS的基本组成单元,用于封装代码块以实现特定功能。理解函数的定义、调用和参数传递方式非常重要。具体案例详解
js函数:函数是JS的基本组成单元,用于封装代码块以实现特定功能。理解函数的定义、调用和参数传递方式非常重要。具体案例详解
33 1
|
4月前
|
存储 JavaScript 前端开发
对象和类:JS是一种基于对象的语言,可以创建和使用自定义对象。ES6引入了类的概念,使得面向对象编程更加方便。
对象和类:JS是一种基于对象的语言,可以创建和使用自定义对象。ES6引入了类的概念,使得面向对象编程更加方便。
25 0
|
5月前
简化对象和函数写法
简化对象和函数写法
19 1