利用cJSON解析JSON格式

3 篇文章 1 订阅
3 篇文章 0 订阅

目录

一、JSON格式

二、cJSON下载

三、cJSON常用函数接口

四、cJSON解析JSON案例

1.一层键值

2.多层键值(两次为例)

3.json数组解析

五、JSON添加数据 (与链表类似)

三层键值


在做的项目需要发JSON格式的消息并解析,因此学习了利用cJSON解析JSON格式,该格式易于人阅读和编写。同时也易于机器解析和生成。

一、JSON格式

语法:键 / 值

1、以 { 开始,以 } 结束,允许嵌套使用

2、每个键和值成对出现,并使用:分隔。如"age"=23

3、键值对之间用 ,分隔

值的多种类型:

字符串:用 " "

{
    "name":"code",
    "gender":"male"
}

数字:整数或浮点数都直接表示

{
    "key1":10,
    "key2":20.0
}

数组:用[ ]

 {
     "key1" : [0, 1],
     "key2" : [2, 3]
 }

布尔值:fault、true

二、cJSON下载

gitee仓库:https://gitee.com/peng-jiaweibabe/c-json

git clone https://gitee.com/peng-jiaweibabe/c-json.git

cJSON的.c和.h文件,使用的时候,只需要将这两个文件复制到工程目录,然后包含头文件cJSON.h即可。即#include "cJSON.h"

如若出现该情况,链接math库即可 

 

三、cJSON常用函数接口

1.cJSON_Parse

CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value)

函数功能:将一个JSON字符串,按照cJSON结构体的结构序列化整个数据包,并在堆中开辟一块内存存储cJSON结构体

返回值:成功返回一个指向内存块中的cJSON的指针,失败返回NULL

2.cJSON_Print

CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item)    //按JSON格式打印
    
CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item)    //不按JSON格式打印

函数功能:将整条链表中存放的JSON信息输出到一个字符串中,使用时只需用一个字符串指针(char *)接收该函数返回的指针地址即可。

返回值:成功返回一个char*指针并指向位于堆中JSON字符串,失败返回NULL

3.cJSON_Delete

CJSON_PUBLIC(void) cJSON_Delete(cJSON *c)

函数功能:释放位于堆中cJSON结构体内存

返回值:无

4.cJSON_GetObjectItem

(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string)

函数功能:根据键值对的名称从链表中取出对应的值,返回该键值对(链表节点)的地址

返回值:成功返回一个指向内存块中的cJSON的指针,失败返回NULL

5.cJSON_GetObjectItem(数组相关)


CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string)

(int) cJSON_GetArraySize(const cJSON *array)

(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index)

6.创建对象函数接口

/* raw json */
CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw);
CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void);
CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void);
/* These calls create a cJSON item of the appropriate type. */
CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void);
CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void);
CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void);
CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean);
CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num);
CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string);
/* These utilities create an Array of count items. */
CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count);
CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count);
CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count);
CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char **strings, int count);

7.添加cJSON对象到链表

CJSON_PUBLIC(void) cJSON_AddItemToArray(cJSON *array, cJSON *item);

CJSON_PUBLIC(void) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item);

8.从现有的cJSON链表中删除一个对象

CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item);
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which);
CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which);
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string);
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string);
CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string);
CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string);

四、cJSON解析JSON案例

1.一层键值

/*********************************************************************************
 *      Copyright:  (C) 2022 Nbiot<lingyun@gail.com>
 *                  All rights reserved.
 *
 *       Filename:  test_cjson.c
 *    Description:  This file test_cjson.c
 *
 *        Version:  1.0.0(30/05/22)
 *         Author:  Nbiot <lingyun@gail.com>
 *      ChangeLog:  1, Release initial version on "30/05/22 20:25:49"
 *
 ********************************************************************************/
#include <stdio.h>
#include <string.h>
#include "cJSON.h"

int main (int argc, char **argv)
{
        char json_buf[] = "{\"type\":\"text\",\"number\":1111,\"sms\":\"nihao\"}";

        cJSON *json = NULL;
        cJSON *json_type = NULL;
        cJSON *json_num = NULL;
        cJSON *json_sms = NULL;

        printf("json格式化前:\n");
        printf("%s\n\n", json_buf);

        json = cJSON_Parse(json_buf);   //json格式序列化

        if (NULL == json)
        {
                printf("cJSON_Parse error:%s\n", cJSON_GetErrorPtr());
        }

        printf("json格式化后:\n");
        printf("%s\n\n",cJSON_Print(json));

        /* 获取相应key的value */
        json_type = cJSON_GetObjectItem(json, "type");
        json_num = cJSON_GetObjectItem(json, "number");
        json_sms = cJSON_GetObjectItem(json, "sms");

        printf("type:%s\n", json_type->valuestring);
        printf("number:%d\n", json_num->valueint);
        printf("sms:%s\n", json_sms->valuestring);

        cJSON_Delete(json);             //释放cjson结构体内存

        return 0;
}

结果:

 2.多层键值(两次为例)

/*********************************************************************************
 *      Copyright:  (C) 2022 Nbiot<lingyun@gail.com>
 *                  All rights reserved.
 *
 *       Filename:  test_cjson1.c
 *    Description:  This file test_cjson1.c
 *
 *        Version:  1.0.0(30/05/22)
 *         Author:  Nbiot <lingyun@gail.com>
 *      ChangeLog:  1, Release initial version on "30/05/22 23:36:09"
 *
 ********************************************************************************/

#include <stdio.h>
#include <string.h>
#include "cJSON.h"

int main (int argc, char **argv)
{
        char json_buf[] =  "{\"type\":\"text\",\"number\":{\"phone_number\":\"17687499242\"},\"sms\":\"nihao\"}";

        cJSON *json = NULL;
        cJSON *json_phone_number = NULL;

        printf("json格式化前:\n");
        printf("%s\n\n", json_buf);

        json = cJSON_Parse(json_buf);   //json格式序列化

        if (NULL == json)
        {
                printf("cJSON_Parse error:%s\n", cJSON_GetErrorPtr());
        }

        printf("json格式化后:\n");
        printf("%s\n\n",cJSON_Print(json));

        /* 获取相应key的value */
        json_phone_number = cJSON_GetObjectItem(json, "number");    //首先获取第一次键值

        json_phone_number = cJSON_GetObjectItem(json_phone_number, "phone_number");    //获取第二层

        printf("phone_number:%s\n", json_phone_number->valuestring);

        cJSON_Delete(json);             //释放cjson结构体内存

        return 0;
}

结果:

3.json数组解析

/*********************************************************************************
 *      Copyright:  (C) 2022 Nbiot<lingyun@gail.com>
 *                  All rights reserved.
 *
 *       Filename:  test_cjson3.c
 *    Description:  This file test_cjson3.c
 *
 *        Version:  1.0.0(31/05/22)
 *         Author:  Nbiot <lingyun@gail.com>
 *      ChangeLog:  1, Release initial version on "31/05/22 00:14:13"
 *
 ********************************************************************************/

#include <stdio.h>
#include <string.h>
#include "cJSON.h"

int main (int argc, char **argv)
{
        char json_buf[] = "{\"type\":\"text\",\"number\":1111,\"sms\":\"nihao\",\"array\":[1,2,3]}";

        cJSON *json = NULL;
        cJSON *json_array = NULL;
        int array_size=0;
        cJSON *json_array_value = NULL;

        printf("json格式化前:\n");
        printf("%s\n\n", json_buf);

        json = cJSON_Parse(json_buf);   //json格式序列化

        if (NULL == json)
        {
                printf("cJSON_Parse error:%s\n", cJSON_GetErrorPtr());
        }

        printf("json格式化后:\n");
        printf("%s\n\n",cJSON_Print(json));

        json_array = cJSON_GetObjectItem(json, "array");    
        array_size = cJSON_GetArraySize(json_array);    //获取数组大小

        printf("array_size=%d\n",array_size);

        for(int i=0; i<array_size; i++)
        {
                json_array_value = cJSON_GetArrayItem(json_array, i);
                printf("array[%d]=%d\n", i,json_array_value->valueint);
        }

        cJSON_Delete(json);             //释放cjson结构体内存

        return 0;
}

 结果:

五、JSON添加数据 (与链表类似)

三层键值

/*********************************************************************************
 *      Copyright:  (C) 2022 Nbiot<lingyun@gail.com>
 *                  All rights reserved.
 *
 *       Filename:  test_cjson2.c
 *    Description:  This file test_cjson2.c
 *
 *        Version:  1.0.0(30/05/22)
 *         Author:  Nbiot <lingyun@gail.com>
 *      ChangeLog:  1, Release initial version on "30/05/22 20:46:57"
 *
 ********************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include "cJSON.h"

int main(int argc, char **argv)
{
        char *json_ptr = NULL;
        cJSON * root   =  cJSON_CreateObject();
        cJSON * son    =  cJSON_CreateObject();
        cJSON * next   =  cJSON_CreateObject();

        cJSON_AddItemToObject(root, "gender", cJSON_CreateString("male"));
        cJSON_AddItemToObject(root, "student", son); //第一层嵌套键值
        cJSON_AddItemToObject(son,      "name", cJSON_CreateString("xiaochen"));//第二层嵌套键值
        cJSON_AddItemToObject(son,      "school", next); //第二层嵌套键值
        cJSON_AddItemToObject(next, "name", cJSON_CreateString("high school"));//第三层嵌套键值

        json_ptr = cJSON_Print(root);
        printf("JSON:\n", json_ptr);
        printf("%s\n", json_ptr);

        free(json_ptr);

        cJSON_Delete(root);

        return 0;
}

结果:

cJSON是一个用于解析和生成JSON数据的C语言库。它提供了一组API函数,使得用户可以方便地解析和生成JSON数据。以下是cJSON库中常用的解析接口: 1. cJSON_Parse:将JSON字符串解析成cJSON节点 ``` cJSON *cJSON_Parse(const char *value); ``` cJSON_Parse函数接受一个JSON字符串作为输入参数,并返回一个cJSON节点。如果解析成功,则返回的节点表示整个JSON文档的根节点;否则返回NULL。调用者需要手动释放返回的节点。 2. cJSON_GetObjectItem:获取对象节点中指定名称的子节点 ``` cJSON *cJSON_GetObjectItem(const cJSON *object, const char *string); ``` cJSON_GetObjectItem函数接受两个参数:对象节点和字符串名称。如果对象节点是一个JSON对象(即类型为cJSON_Object),则返回该对象中名称为string的子节点;否则返回NULL。 3. cJSON_GetArrayItem:获取数组节点中指定索引的子节点 ``` cJSON *cJSON_GetArrayItem(const cJSON *array, int index); ``` cJSON_GetArrayItem函数接受两个参数:数组节点和索引值。如果数组节点是一个JSON数组(即类型为cJSON_Array),则返回该数组中索引为index的子节点;否则返回NULL。 4. cJSON_GetStringValue:获取字符串节点的值 ``` const char *cJSON_GetStringValue(const cJSON *object); ``` cJSON_GetStringValue函数接受一个参数:字符串节点。如果字符串节点是一个JSON字符串(即类型为cJSON_String),则返回该节点中的字符串值;否则返回NULL。 以上是常用的cJSON解析接口,当然还有很多其他的接口可以帮助用户完成JSON数据的解析和生成。在使用cJSON库时,需要仔细阅读cJSON.h头文件中的API文档,以便正确地使用各种接口。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值