cjson嵌套

该代码示例展示了如何在C语言中使用cJSON库创建嵌套的JSON对象,并进行解析。函数`makeJson`创建了一个包含多个字符串键值对及子对象的JSON字符串,而`parseJson`函数则负责解析这个JSON字符串,提取并打印出对应的值。
摘要由CSDN通过智能技术生成

创建

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;
}

结果:


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]

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值