【C语言实用库】cJSON-解析json数据的利器

cJSON是用于解析json格式字符串的一套api,非常好用,下面介绍一下使用方法:

1. json介绍

json中一般是"key":value的形式,而value有一共7种类型,下面是cJSON中对几种类型的宏定义:

#define cJSON_False 0
#define cJSON_True 1
#define cJSON_NULL 2
#define cJSON_Number 3
#define cJSON_String 4
#define cJSON_Array 5
#define cJSON_Object 6

本文也是将上面7种类型全部包括,分别介绍如何获取key对应的value值,以下是本文用到的json格式字符串,其中将数值分为整数和小数,另外添加了一个对象数组(参见朋友)

const char *str_xy = "{\
                \"姓名\":\"小一\",\
                \"年龄\":25,\
                \"身高\":175.6,\
                \"爱好\":[\"读书\",\"写字\",\"唱歌\"],\
                \"家人\": {\"哥哥\":\"小法\",\"姐姐\":\"潇潇\"},\
                \"朋友\": [{\"姓名\":\"小丽\",\
                        \"是否同学\":true},\
                        {\"姓名\":\"小风\",\
                        \"是否同学\":false}],\
                \"修仙等级\":null\
                }";

2. json解析

2.1 将字符串解析为json格式数据

cJSON *json_str_xy = cJSON_Parse(str_xy);
// --判断是否可以解析为json
if(json_str_xy == NULL) {
    printf("字符串不是标准的json格式!\n");
    // --释放空间,见文章最后
    cJSON_Delete(json_str_xy);
}

2.2 解析字符串(姓名)

cJSON *json_name = cJSON_GetObjectItem(json_str_xy, "姓名");
if(json_name != NULL && json_name->type == cJSON_String) {
    printf("姓名 = %s\n", json_name->valuestring);
}

2.3 解析数值(年龄和身高)

cJSON *json_age = cJSON_GetObjectItem(json_str_xy, "年龄");
if(json_age != NULL && json_age->type == cJSON_Number) {
    printf("年龄 = %d\n", json_age->valueint);
}
cJSON *json_high = cJSON_GetObjectItem(json_str_xy, "身高");
if(json_high != NULL && json_age->type == cJSON_Number) {
    printf("身高 = %0.2f\n", json_high->valuedouble);
}

2.4 解析数组(爱好)

cJSON *cjson_hobby = cJSON_GetObjectItem(json_str_xy, "爱好");
cJSON *cjson_hobby_item = NULL;
if(cjson_hobby != NULL && cjson_hobby->type == cJSON_Array) {
    int hobby_num = cJSON_GetArraySize(cjson_hobby);
    printf("爱好 =");
    for (int i = 0; i < hobby_num; i++) {
        cjson_hobby_item = cJSON_GetArrayItem(cjson_hobby, i);
        if(cjson_hobby_item != NULL && cjson_hobby_item->type == cJSON_String) {
            printf(";%s", cjson_hobby_item->valuestring);
        }
    }
    printf("\n");
}

2.5 解析对象(家人)

cJSON *json_family = cJSON_GetObjectItem(json_str_xy, "家人");
if(json_family != NULL && json_family->type == cJSON_Object) {
    printf("家人 = ");

    cJSON *json_brother = cJSON_GetObjectItem(json_family, "哥哥");
    if(json_brother != NULL && json_brother->type == cJSON_String) {
        printf("哥哥是%s", json_brother->valuestring);
    }
    cJSON *json_sister = cJSON_GetObjectItem(json_family, "姐姐");
    if(json_sister != NULL && json_sister->type == cJSON_String) {
        printf(";姐姐是%s\n", json_sister->valuestring);
    }
}

2.6 解析数组中嵌套对象

cJSON *json_friends = cJSON_GetObjectItem(json_str_xy, "朋友");
cJSON *json_friends_item = NULL;
if(json_friends != NULL && json_friends->type == cJSON_Array) {
    int friends_num = cJSON_GetArraySize(json_friends);
    cJSON **friends_name = malloc(sizeof(cJSON *) * friends_num);
    cJSON **friends_type = malloc(sizeof(cJSON *) * friends_num);
    printf("朋友 =");
    for (int i = 0; i < friends_num; i++) {
        json_friends_item = cJSON_GetArrayItem(json_friends, i);
        if(json_friends_item != NULL && json_friends_item->type == cJSON_Object) {
            friends_name[i] = cJSON_GetObjectItem(json_friends_item, "姓名");
            if(friends_name[i] != NULL && friends_name[i]->type == cJSON_String) {
                printf(" %s", friends_name[i]->valuestring);
            }
            friends_type[i] = cJSON_GetObjectItem(json_friends_item, "是否同学");
            if(friends_type[i] != NULL) {
                if(friends_type[i]->type == cJSON_True) {
                    printf("是同学;");
                }
                if(friends_type[i]->type == cJSON_False) {
                    printf("不是同学;");
                }
            }
        }
    }
    free(friends_name);
    free(friends_type);
}

2.7 解析null(修仙)

if(json_xiuxian != NULL && json_xiuxian->type == cJSON_NULL) {
    printf("\n修仙 = 不如信仰马克思\n");
}

2.8 释放空间(很重要,不操作会造成内存泄漏)

cJSON_Delete(json_str_xy);

3. 创建json数据

json数据的创建也很重要,所以下面将创建文章开头定义的字符串所对应的json数据

3.1 创建对象

cJSON *json_str_xy = cJSON_CreateObject();

3.2 添加字符串对象

cJSON_AddStringToObject(json_str_xy, "姓名", "小一");

3.3 添加数值型对象

cJSON_AddNumberToObject(json_str_xy, "年龄", 25);
cJSON_AddNumberToObject(json_str_xy, "身高", 175.6);

3.4 添加数组对象

const char *hobby[3] = {"读书","写字","唱歌"};
cJSON *json_hobby = cJSON_CreateStringArray(hobby, 3);
cJSON_AddItemToObject(json_str_xy, "爱好", json_hobby);

3.5 添加子对象

cJSON *json_family = cJSON_CreateObject();
cJSON_AddStringToObject(json_family, "哥哥", "小法");
cJSON_AddStringToObject(json_family, "姐姐", "潇潇");
cJSON_AddItemToObject(json_str_xy, "家人", json_family);

3.6 添加对象数组

cJSON *json_friend_1 = cJSON_CreateObject();
cJSON_AddStringToObject(json_friend_1, "姓名", "小明");
cJSON_AddBoolToObject(json_friend_1, "是否同学", cJSON_True);
cJSON *json_friend_2 = cJSON_CreateObject();
cJSON_AddStringToObject(json_friend_2, "姓名", "小李");
cJSON_AddBoolToObject(json_friend_2, "是否同学", cJSON_False);
cJSON *json_friend_array = cJSON_CreateArray();
cJSON_AddItemToArray(json_friend_array, json_friend_1);
cJSON_AddItemToArray(json_friend_array, json_friend_2);

cJSON_AddItemToObject(json_str_xy, "朋友", json_friend_array);

3.7 添加null对象

cJSON_AddNullToObject(json_str_xy, "修仙");

4. 结束语

以上就是json对象的解析与创建,你学废了么?

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值