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

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

简介

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


相关文章
|
6天前
|
存储 JSON 数据处理
从JSON数据到Pandas DataFrame:如何解析出所需字段
从JSON数据到Pandas DataFrame:如何解析出所需字段
19 1
|
6天前
|
XML JavaScript 数据格式
Beautiful Soup 库的工作原理基于解析器和 DOM(文档对象模型)树的概念
【5月更文挑战第10天】Beautiful Soup 使用解析器(如 html.parser, lxml, html5lib)解析HTML/XML文档,构建DOM树。它提供方法查询和操作DOM,如find(), find_all()查找元素,get_text(), get()提取信息。还能修改DOM,添加、修改或删除元素,并通过prettify()输出格式化字符串。它是处理网页数据的利器,尤其在处理不规则结构时。
38 2
|
6天前
|
JSON 数据格式 索引
python之JMESPath:JSON 查询语法库示例详解
python之JMESPath:JSON 查询语法库示例详解
16 0
|
6天前
|
JSON Java Linux
【探索Linux】P.30(序列化和反序列化 | JSON序列化库 [ C++ ] )
【探索Linux】P.30(序列化和反序列化 | JSON序列化库 [ C++ ] )
23 2
|
6天前
|
JSON 安全 Swift
【Swift开发专栏】Swift中的JSON解析与处理
【4月更文挑战第30天】本文介绍了Swift中的JSON解析与处理。首先,讲解了JSON的基础,包括其键值对格式和在Swift中的解析与序列化方法。接着,展示了如何使用`Codable`协议简化JSON操作,以及处理复杂结构的示例。通过这些内容,读者能掌握在Swift中高效地处理JSON数据的方法。
|
6天前
|
分布式计算 DataWorks 关系型数据库
DataWorks产品使用合集之在DataWorks中,使用JSON解析函数将MySQL表中的字段解析成多个字段将这些字段写入到ODPS(MaxCompute)中如何解决
DataWorks作为一站式的数据开发与治理平台,提供了从数据采集、清洗、开发、调度、服务化、质量监控到安全管理的全套解决方案,帮助企业构建高效、规范、安全的大数据处理体系。以下是对DataWorks产品使用合集的概述,涵盖数据处理的各个环节。
30 3
|
6天前
|
JSON 前端开发 Java
Json格式数据解析
Json格式数据解析
|
4天前
|
JSON NoSQL MongoDB
实时计算 Flink版产品使用合集之要将收集到的 MongoDB 数据映射成 JSON 对象而非按字段分割,该怎么操作
实时计算Flink版作为一种强大的流处理和批处理统一的计算框架,广泛应用于各种需要实时数据处理和分析的场景。实时计算Flink版通常结合SQL接口、DataStream API、以及与上下游数据源和存储系统的丰富连接器,提供了一套全面的解决方案,以应对各种实时计算需求。其低延迟、高吞吐、容错性强的特点,使其成为众多企业和组织实时数据处理首选的技术平台。以下是实时计算Flink版的一些典型使用合集。
35 1
|
6天前
|
XML JSON API
转Android上基于JSON的数据交互应用
转Android上基于JSON的数据交互应用
11 1
|
6天前
|
JSON JavaScript Java
从前端Vue到后端Spring Boot:接收JSON数据的正确姿势
从前端Vue到后端Spring Boot:接收JSON数据的正确姿势
26 0

推荐镜像

更多