cJSON简析

2023-12-27 00:28
文章标签 简析 cjson

本文主要是介绍cJSON简析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

    • json概要
    • cJSON数据结构
  • 递归解析
  • 示例
    • references

json概要

json是一种文本格式的协议
对于人的可阅读性非常好

在这里插入图片描述
其中object和array中的value都可以嵌套

cJSON数据结构

每个节点的数据结构如下

/* 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 */#define cJSON_IsReference 256
#define cJSON_StringIsConst 512/* The cJSON structure: */
typedef struct cJSON
{/* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */struct cJSON *next;struct cJSON *prev;/* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */struct cJSON *child;/* The type of the item, as above. */int type;/* The item's string, if type==cJSON_String  and type == cJSON_Raw */char *valuestring;/* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */int valueint;/* The item's number, if type==cJSON_Number */double valuedouble;/* The item's name string, if this item is the child of, or is in the list of subitems of an object. */char *string;
} cJSON;

递归解析

入口函数:
cJSON_ParseWithLengthOpts(...)
{parse_value(...) // 解析值
}
parse_value(...)
{if(...) { ->type = cJSON_NULL; return true; }if(...) { ->type = cJSON_False; return true; }if(...) { ->type = cJSON_True; return true; }if(...) { return parse_string(...); }if(...) { return parse_number(...); }if(...) { return parse_array(...); }if(...) { return parse_object(...); }}
parse_string(...)
{...
}
parse_number(...)
{...
}
parse_array(...)
{do {...parse_value(...);...} while(... && x==',');...
}
parse_object(...)
{do {...cJSON *new_item = cJSON_New_Item();current_item->next = new_item;new_item->prev = current_item;current_item = new_item;parse_string(current_item, input_buffer);current_item->string = current_item->valuestring;current_item->valuestring = NULL;parse_value(...);...} while(... && x==',');...
}

示例

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

对应的树状结构:
在这里插入图片描述

node1:

struct cJSON *next = 0
struct cJSON *prev = 0
struct cJSON *child = node2
int type = cJSON_Object
char *valuestring = 0
int valueint = 0
double valuedouble = 0
char *string = 0

node2:

struct cJSON *next = node3
struct cJSON *prev = 0
struct cJSON *child = 0
int type = cJSON_String;
char *valuestring = alloc("Awesome 4K")
int valueint = 0
double valuedouble = 0
char *string = alloc("name")

node3:

struct cJSON *next = 0
struct cJSON *prev = node2
struct cJSON *child = node4
int type = cJSON_Array
char *valuestring = 0
int valueint = 0
double valuedouble = 0
char *string = alloc("resolutions")

node4:

struct cJSON *next = node5
struct cJSON *prev = 0
struct cJSON *child = node7
int type = cJSON_Object
char *valuestring = 0
int valueint = 0
double valuedouble = 0
char *string = 0

node5:

struct cJSON *next = node6
struct cJSON *prev = node4
struct cJSON *child = node9
int type = cJSON_Object
char *valuestring = 0
int valueint = 0
double valuedouble = 0
char *string = 0

node6:

struct cJSON *next = 0
struct cJSON *prev = node5
struct cJSON *child = node11
int type = cJSON_Object
char *valuestring = 0
int valueint = 0
double valuedouble = 0
char *string = 0

node7:

struct cJSON *next = node8
struct cJSON *prev = 0
struct cJSON *child = 0
int type = cJSON_Number
char *valuestring = 0
int valueint = 1280
double valuedouble = 1280
char *string = alloc("width")

node8:

struct cJSON *next = 0
struct cJSON *prev = node7
struct cJSON *child = 0
int type = cJSON_Number
char *valuestring = 0
int valueint = 720
double valuedouble = 720
char *string = alloc("height")

node9:

struct cJSON *next = node10
struct cJSON *prev = 0
struct cJSON *child = 0
int type = cJSON_Number
char *valuestring = 0
int valueint = 1920
double valuedouble = 1920
char *string = alloc("width")

node10:

struct cJSON *next = 0
struct cJSON *prev = node9
struct cJSON *child = 0
int type = cJSON_Number
char *valuestring = 0
int valueint = 1080
double valuedouble = 1080
char *string = alloc("height")

node11:

struct cJSON *next = node12
struct cJSON *prev = 0
struct cJSON *child = 0
int type = cJSON_Number
char *valuestring = 0
int valueint = 3840
double valuedouble = 3840
char *string = alloc("width")

node12:

struct cJSON *next = 0
struct cJSON *prev = node11
struct cJSON *child = 0
int type = cJSON_Number
char *valuestring = 0
int valueint = 2160
double valuedouble = 2160
char *string = alloc("height")

references

https://github.com/DaveGamble/cJSON/tree/master

这篇关于cJSON简析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/541299

相关文章

ElasticSearch底层原理简析

1.ElasticSearch简述 ElastiaSearch(以下简称ES)是一个基于Lucene的搜索服务器,它提供了一个分布式多用户能力的全文搜索引擎,支持RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。ES设计用于云计算中,能够进行实时搜索,支持PB级搜索,具有稳定,可靠,快速,安装使用方便等

cJSON V1.4.5源码

json官网: http://www.json.orgJSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is ba

【C语言实用库】cJSON-解析json数据的利器

cJSON是用于解析json格式字符串的一套api,非常好用,下面介绍一下使用方法: 1. json介绍 json中一般是"key":value的形式,而value有一共7种类型,下面是cJSON中对几种类型的宏定义: #define cJSON_False 0#define cJSON_True 1#define cJSON_NULL 2#define cJSON_Number 3

cjson介绍-应用-实例-源码分析

1.json简介 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。 易于人阅读和编写。同时也易于机器解析和生成。 它基于JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999的一个子集。 JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言

Swing之EDT简析

Java Swing 是一个单线程图形库,里面绝大多数的代码不是线程安全的(thread-safe),Swing组件大多数没有做同步线程安全处理,也就是说任何地方都能随便调用,在不同的线程里面随便使用这些API去更新界面元素和设置值,都可能会出现一些问题。 Swing框架使用了Event Queue和EDT(Event-Dispatch-Thread)来保证线程是安全的。在GUI界面上发出的请求

面向对象程序设计之链表 list 的简析(C++)

简介:链表是一个双向的结构,与string与vector不同的是他不支持[]访问,因为链表是由一个节点一个节点连接而成的,并不连续。我们可以在常数量级内对于链表进行插入与删除数据 1.构造函数 我们在cplusplus.com中可以查到链表总共有四种构造的方式:1.无参构造(默认构造);2.使用n个val构造;3.迭代器区间构造;4.拷贝构造 接下来让我们简单创建一个链表

使用cJSON创建JSON字符串,举例详解。(六)

使用cJSON创建JSON字符串     在Linux下,使用C语言编程,开始JSON字符串的创建。我们还是一步步来,逐渐由简单到复制。   1,下载源码 可以从如下网站来下载:https://sourceforge.net/projects/cjson/ 。 2,包含cJSON的源码 下载下来,解压后,从里面找到两个文件(cJSON.c、cJSON.h),复制到我们的工程里面。只需在函

【cJson】cJSON的构造和解析(五)

对于cJSON的使用,我主要是用来模拟远程服务器端返回的一个json类型的目录结构,客户端进行获取并进行解析,把解析出来的目录按照原本的结构显示在本地。 cJSON是一个超轻巧,携带方便,单文件,简单的可以作为ANSI-C标准的JSON解析器。 进入cJSON.h头文件中可以查看cJSON的相关信息。主要包括:cJSON结构体、cJSON类型、cJSON的一些内部的函数等。 // cJSO

【cJson】 JSON格式详解(二)

摘要: 0.前言     JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。 易于人阅读和编写。同时也易于机器解析和生成。 它基于JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999的一个子集。 0.前言 JSON(JavaScript Ob

【cJSON】CJSON学习笔记(四)

来自https://yq.aliyun.com/articles/23908?spm=5176.100239.blogcont23909.22.b57b266nePTks 摘要: 1.重要函数说明     【1】两个创建     【创建JSON对象】cJSON *cJSON_CreateObject(void);     【创建JSON数组】cJSON *cJSON_CreateArray