温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C语言怎么用cJSON解析JSON格式

发布时间:2023-04-21 15:16:01 来源:亿速云 阅读:98 作者:iii 栏目:开发技术

这篇文章主要介绍“C语言怎么用cJSON解析JSON格式”,在日常操作中,相信很多人在C语言怎么用cJSON解析JSON格式问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C语言怎么用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下载

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

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

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

C语言怎么用cJSON解析JSON格式

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

结果:

C语言怎么用cJSON解析JSON格式

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

结果:

C语言怎么用cJSON解析JSON格式

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

 结果:

C语言怎么用cJSON解析JSON格式

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

结果:

C语言怎么用cJSON解析JSON格式

到此,关于“C语言怎么用cJSON解析JSON格式”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI