最近工作中用到了一个叫 cJSON 的轻量级 JSON 解析器,感觉挺好用的,就做个笔记记录一下。
1 概述
cJSON 是一个轻量级的开源 JSON 解析器,它使用的是 ANSI C 标准,LICENSE 是 MIT。
github 地址在: https://github.com/DaveGamble/cJSON
使用方法很简单,直接把项目下的cJSON.c
和cJSON.h
文件拷贝出来就行了,所有的解析方法和结构体都在这两个文件里,名副其实的轻量级:)。
在头文件中:
1 |
|
不嫌麻烦也可以 make 一下,
1 |
|
make 后,安装完,会将头文件安装到/usr/local/include/cjson
将库安装到/usr/local/lib
,同样 include 一下。
1 |
|
2 结构体
cJSON 中将 json 数据抽象成结构体cJSON
1 |
|
可以看到,在 cJSON 中将 json 中的 value 粗糙地分成了三种类型valuestring
、valueint
和valuedouble
,不过这三种类型已经能够满足绝大多数需求。json 的 key 使用string
表示。
type
使用了一个比特位来表示接收到的不同数据类型
1 |
|
结合type
可以判断更多的数据类型,包括 bool 值。使用cJSON_is...
方法来判断是否为对应类型。
对应cJSON_is...
方法判断类型,可以使用cJSON_Create...
方法创建对应的类型,返回一个对应类型的cJSON
。
3 解析 JSON
解析一个以 0 结尾的 JSON 字符串,直接使用cJSON_Parse
1 |
|
这个方法会解析 string,并且生成一颗cJSON
树。使用完后要使用cJSON_Delete
释放它。
获得 cJSON 结构体之后,就可以对结构体做各种相应的操作了,比如cJSON_GetObjectItem
或cJSON_GetObjectItemCaseSensitive
从 child 里面找一个固定 string 的 cJSON。
1 |
|
cJSON_HasObjectItem
判断 json 中是否有对应 string 的项。
cJSON 提供三个方面的方法:解析 json、组合 json、打印 json,具体内容查看 cJSON.h 头文件就一目了然了。
4 解析对照图
以官网的字符串为例:
1 |
|
解析后,在内存结构体中如图:cJSON