【嵌入式开源库】cJSON的使用,高效精简的json解析库

35 篇文章 9 订阅
6 篇文章 5 订阅

简介

JSON 全称 JavaScript Object Notation,即 JS对象简谱,是一种轻量级的数据格式。

它采用完全独立于编程语言的文本格式来存储和表示数据,语法简洁、层次结构清晰,易于人阅读和编写,同时也易于机器解析和生成,有效的提升了网络传输效率。

cJSON是一个使用C语言编写的JSON数据解析器并采用ANSI C(C89)编写以支持尽可能多的平台和编译器,该项目同时也具有超轻便,可移植,单文件的特点,使用MIT开源协议。

json格式

{
    "name": "Awesome 4K",
    "resolutions": [
        {
            "width": 1280,
            "height": 720
        },
        {
            "width": 1920,
            "height": 1080
        },
        {
            "width": 3840,
            "height": 2160
        }
    ]
}

本章使用环境:
虚拟机ubuntu1804

下载

cJSON项目托管在Github,下载地址:https://github.com/DaveGamble/cJSON
在这里插入图片描述
也可以通过git工具进行项目克隆(保证自己电脑配好git环境)

git clone https://github.com/DaveGamble/cJSON.git

使用介绍

因为整个库只有一个C文件和一个头文件,所以我们只需要复制cJSON.h和cJSON.c到项目中就可以使用了。

工程创建

mkdir study_json
cd study_json/
touch main.c
cp ../cJSON/cJSON.c ./
cp ../cJSON/cJSON.h ./

main.c中内容

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

int main(int argc, char *argv[])
{
	printf("hello json!\n");
	return 0;
}

编译指令

gcc main.c cJSON.c -o app
// 执行
./app

然后我们来简单看一下cjson的源代码,

cJSON结构体

在源代码中我们可以找到一个结构体是用来存放我们将要打包或者解析的数据,结构体如下

/* The cJSON structure: */
typedef struct cJSON
{
    struct cJSON *next;
    struct cJSON *prev;
    struct cJSON *child;
    int type;
    char *valuestring;
    /* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */
    int valueint;
    double valuedouble;
    char *string;
} cJSON;

从结构体看来可以看出来cJSON的数据存储是采用链表的方式存储的(next指向下一个键值对,prev指向上一个键值对,child指针是用来存储子object对象),这就涉及到一个释放的问题,刚开始使用很容易忽略这个问题最后导致程序跑飞问题,通过结构体的数据变量可以看到cjson支持的数据类型有bool型、字符串类型、int类型、双进精度浮点数(最后的char *string表示的是对象key的名称并非值value对象);然后我们来看一下.h文件给我们提供了那些可以操作的接口;

节点创建

// 创建指定类型的json对象
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);
CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw);
CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void);
CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void);
CJSON_PUBLIC(cJSON *) cJSON_CreateStringReference(const char *string);
CJSON_PUBLIC(cJSON *) cJSON_CreateObjectReference(const cJSON *child);
CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child);
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 *const *strings, int count);

添加节点到对象

// 将创建好的json数据添加到object中
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item);
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item);
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item);
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item);
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item);

// 创建数据同时添加到object中去
CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * const name);
CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * const name);
CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * const name);
CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, const char * const name, const cJSON_bool boolean);
CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number);
CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string);
CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * const name, const char * const raw);
CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char * const name);
CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * const name);

内存释放

// 将链表数据输出为char *字符串,也就是打印格式
cJSON_Print(const cJSON *item);
// 释放内存
cJSON_free(void *object);
cJSON_Delete(cJSON *item);

这三个函数都非常重要,第一个是我们把创建好的cjson数据给输出成字符串然后供我们使用,当我们使用完成过后一定要使用cJSON_free函数进行释放,因为print这个函数也是通过申请内存的方式来制造这个字符串的,delete这个函数是用来释放我们的object的,和释放链表的方式一样,我们只需要释放根节点就可以了。
在这里插入图片描述
同时cJSON支持自定义malloc函数和free函数,如果我们是在单片机等平台上使用了外部sram芯片,就可以使用下面这个接口将外部sram的malloc接口和free接口初始化上去

typedef struct cJSON_Hooks
{
      /* malloc/free are CDECL on Windows regardless of the default calling convention of the compiler, so ensure the hooks allow passing those functions directly. */
      void *(CJSON_CDECL *malloc_fn)(size_t sz);
      void (CJSON_CDECL *free_fn)(void *ptr);
} cJSON_Hooks;

(void) cJSON_InitHooks(cJSON_Hooks* hooks);

节点类型判断

// 解析json对象是属于上面类型的api函数
cJSON_IsInvalid(const cJSON * const item);
cJSON_IsFalse(const cJSON * const item);
cJSON_IsTrue(const cJSON * const item);
cJSON_IsBool(const cJSON * const item);
cJSON_IsNull(const cJSON * const item);
cJSON_IsNumber(const cJSON * const item);
cJSON_IsString(const cJSON * const item);
cJSON_IsArray(const cJSON * const item);
cJSON_IsObject(const cJSON * const item);
cJSON_IsRaw(const cJSON * const item);

//返回值
/* cJSON Types: */
#define cJSON_Invalid (0)
#define cJSON_False  (1 << 0)
#define cJSON_True   (1 << 1)
#define cJSON_NULL   (1 << 2)
#define cJSON_Number (1 << 3)
#define cJSON_String (1 << 4)
#define cJSON_Array  (1 << 5)
#define cJSON_Object (1 << 6)
#define cJSON_Raw    (1 << 7) /* raw json */

数据解析

// 解析json数据需要用到的函数
(cJSON *) cJSON_Parse(const char *value); // 将json数据解析到cjson结构体中
(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string);// 根据string键值来获取内容

// 如果对象是数组可以用以下函数解析
(int) cJSON_GetArraySize(const cJSON *array);
(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index);

// 获取对象中的数据
CJSON_PUBLIC(char *) cJSON_GetStringValue(const cJSON * const item);
CJSON_PUBLIC(double) cJSON_GetNumberValue(const cJSON * const item);
为了防止堆栈溢出做出的保护措施,在一些小型嵌入式系统中可以给改此值。
#define CJSON_NESTING_LIMIT 1000

节点删除

// 删除一个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);

数据打包json格式

目标:创建下列格式json数据并通过printf打印

{
    "name": "stylle",
    "resolutions": [
        {
            "width": 1280,
            "height": 720
        },
        {
            "width": 1920,
            "height": 1080
        }
    ]
}

main.c

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


int main(int argc, char *argv[])
{
	printf("hello json!\n");

	cJSON *name = NULL;
	cJSON *resolutions = NULL;
	cJSON *resolution = NULL;
    cJSON *width = NULL;
    cJSON *height = NULL;

	//   根节点的创建
	cJSON *root = cJSON_CreateObject();
    if (root == NULL)
    {
		// 内存申请失败,调査到结尾释放根节点
        goto end;
    }
	// name节点
	name = cJSON_CreateString("stylle");
	if(name == NULL)
	{
		goto end;
	}
	cJSON_AddItemToObject(root, "name",name); // 添加到根节点,键值为name

	// 节点数组
	resolutions = cJSON_CreateArray();
	if(resolutions == NULL)
	{
		goto end;
	}
	cJSON_AddItemToObject(root, "resolutions", resolutions);

	// 数组内容[0]
	resolution = cJSON_CreateObject();
	if (resolution == NULL)
	{
		goto end;
	}
	cJSON_AddItemToArray(resolutions, resolution);

	width = cJSON_CreateNumber(1280);
	if (width == NULL)
	{
		goto end;
	}
	cJSON_AddItemToObject(resolution, "width", width);

	height = cJSON_CreateNumber(720);
	if (height == NULL)
	{
		goto end;
	}
	cJSON_AddItemToObject(resolution, "height", height);

	// 数组内容[1]
	resolution = cJSON_CreateObject();
	if (resolution == NULL)
	{
		goto end;
	}
	cJSON_AddItemToArray(resolutions, resolution);

	width = cJSON_CreateNumber(1920);
	if (width == NULL)
	{
		goto end;
	}
	cJSON_AddItemToObject(resolution, "width", width);

	height = cJSON_CreateNumber(1080);
	if (height == NULL)
	{
		goto end;
	}
	cJSON_AddItemToObject(resolution, "height", height);

	// 打印数据
	char *print_buffer = cJSON_Print(root);
	if(print_buffer == NULL)
	{
		goto end;
	}
	printf("%s\n", print_buffer);
	cJSON_free(print_buffer);

end:
	printf("cJSON ERROR : %s\n", cJSON_GetErrorPtr());
	cJSON_Delete(root);

	return 0;
}

在这里插入图片描述

json数据解析

目标:解析下列格式json数据并通过printf打印所有的键值对;

{
        "students":     [{
                        "name": "liulu",
                        "age":  22,
                        "learning":     true
                }, {
                        "name": "wangmazi",
                        "age":  25,
                        "learning":     false
                }]
}

main.c

int main(int argc, char *argv[])
{
	const char *str = "{\"students\":[{\"name\" : \"liulu\", \"age\" : 22, \"learning\" : true},{\"name\" : \"wangmazi\", \"age\" : 25, \"learning\" : false}]}";
    cJSON *root = NULL;
    cJSON *students = NULL, *students1 = NULL, *students2 = NULL;
    cJSON *name1 = NULL, *age1 = NULL, *learning1 = NULL;
    cJSON *name2 = NULL, *age2 = NULL, *learning2 = NULL;

    root = cJSON_Parse(str);

    if (root == NULL){   
        goto end;
    } 
    students = cJSON_GetObjectItem(root, "students");
	if(students == NULL)
	{
		goto end;
	}

    students1 = cJSON_GetArrayItem(students, 0);
	if(students1 == NULL)
	{
		goto end;
	}
    students2 = cJSON_GetArrayItem(students, 1);
	if(students2 == NULL)
	{
		goto end;
	}

	// 数组1内容解析
    name1 = cJSON_GetObjectItem(students1, "name");
	if(name1 == NULL)
	{
		goto end;
	}
    age1 = cJSON_GetObjectItem(students1, "age");
	if(age1 == NULL)
	{
		goto end;
	}
    learning1 = cJSON_GetObjectItem(students1, "learning");
	if(learning1 == NULL)
	{
		goto end;
	}

	// 数组2内容解析
    name2 = cJSON_GetObjectItem(students2, "name");
	if(name2 == NULL)
	{
		goto end;
	}
    age2 = cJSON_GetObjectItem(students2, "age");
	if(age2 == NULL)
	{
		goto end;
	}
    learning2 = cJSON_GetObjectItem(students2, "learning");
	if(learning2 == NULL)
	{
		goto end;
	}

	// 数据打印
    printf("name[0]:%s\n", cJSON_GetStringValue(name1));
    printf("age[0]: %0.f\n", cJSON_GetNumberValue(age1));
    printf("learning [0]: %d\n\n", cJSON_IsTrue(learning1));

    printf("name[1]: %s\n", cJSON_GetStringValue(name2));
    printf("age [1]: %0.f\n", cJSON_GetNumberValue(age2));
    printf("learning [1]: %d\n", cJSON_IsTrue(learning2));

end:
	printf("\ncJSON ERROR : %s\n", cJSON_GetErrorPtr());
    cJSON_Delete(root);

	return 0;
}

在这里插入图片描述

  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凉开水白菜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值