【嵌入式开源库】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;
}


相关文章
|
JSON 缓存 自然语言处理
多语言实时数据微店商品详情API:技术实现与JSON数据解析指南
通过以上技术实现与解析指南,开发者可高效构建支持多语言的实时商品详情系统,满足全球化电商场景需求。
|
存储 JSON 关系型数据库
【干货满满】解密 API 数据解析:从 JSON 到数据库存储的完整流程
本文详解电商API开发中JSON数据解析与数据库存储的全流程,涵盖数据提取、清洗、转换及优化策略,结合Python实战代码与主流数据库方案,助开发者构建高效、可靠的数据处理管道。
|
JSON 算法 API
淘宝商品评论API接口核心解析,json数据返回
淘宝商品评论API是淘宝开放平台提供的数据服务接口,允许开发者通过编程方式获取指定商品的用户评价数据,包括文字、图片、视频评论及评分等。其核心价值在于:
|
JSON 定位技术 PHP
PHP技巧:解析JSON及提取数据
这就是在PHP世界里探索JSON数据的艺术。这场狩猎不仅仅是为了获得数据,而是一种透彻理解数据结构的行动,让数据在你的编码海洋中畅游。通过这次冒险,你已经掌握了打开数据宝箱的钥匙。紧握它,让你在编程世界中随心所欲地航行。
387 67
|
11月前
|
JSON Java Go
【GoGin】(2)数据解析和绑定:结构体分析,包括JSON解析、form解析、URL解析,区分绑定的Bind方法
bind或bindXXX函数(后文中我们统一都叫bind函数)的作用就是将,以方便后续业务逻辑的处理。
574 3
|
11月前
|
XML JSON 数据处理
超越JSON:Python结构化数据处理模块全解析
本文深入解析Python中12个核心数据处理模块,涵盖csv、pandas、pickle、shelve、struct、configparser、xml、numpy、array、sqlite3和msgpack,覆盖表格处理、序列化、配置管理、科学计算等六大场景,结合真实案例与决策树,助你高效应对各类数据挑战。(238字)
1417 0
|
JSON 自然语言处理 API
多语言实时数据淘宝商品评论API:技术实现与JSON数据解析指南
淘宝商品评论多语言实时采集需结合官方API与后处理技术实现。建议优先通过地域站点适配获取本地化评论,辅以机器翻译完成多语言转换。在合规前提下,企业可构建多语言评论数据库,支撑全球化市场分析与产品优化。
|
数据采集 JSON 数据可视化
JSON数据解析实战:从嵌套结构到结构化表格
在信息爆炸的时代,从杂乱数据中提取精准知识图谱是数据侦探的挑战。本文以Google Scholar为例,解析嵌套JSON数据,提取文献信息并转换为结构化表格,通过Graphviz制作技术关系图谱,揭示文献间的隐秘联系。代码涵盖代理IP、请求头设置、JSON解析及可视化,提供完整实战案例。
972 4
JSON数据解析实战:从嵌套结构到结构化表格
|
机器学习/深度学习 JSON 监控
淘宝拍立淘按图搜索与商品详情API的JSON数据返回详解
通过调用taobao.item.get接口,获取商品标题、价格、销量、SKU、图片、属性、促销信息等全量数据。
|
11月前
|
JSON API 数据格式
淘宝拍立淘按图搜索API系列,json数据返回
淘宝拍立淘按图搜索API系列通过图像识别技术实现商品搜索功能,调用后返回的JSON数据包含商品标题、图片链接、价格、销量、相似度评分等核心字段,支持分页和详细商品信息展示。以下是该API接口返回的JSON数据示例及详细解析:

热门文章

最新文章

推荐镜像

更多
  • DNS