博客
关于我
cJSON的那点事儿
阅读量:377 次
发布时间:2019-03-05

本文共 4880 字,大约阅读时间需要 16 分钟。

cJSON的那点事儿

JSON(了解)

JSON(JavaScript Object Notation, JS 对象简谱)是一种轻量级的数据交换格式。它基于 ECMAScript (欧洲计算机协会制定的js规范)的一个子集,采用完全独立于编程语言的文本格式来存储和表示数据。

JSON 的值可以是对象、数组、数字、字符串或者三个字面值 (false、null、true) 中的一个。值中的字面值必须使用小写。数据由键/值对组成,键值对由逗号分隔;花括号表示对象,方括号表示数组。

cJSON(明确)

cJSON 是一个仅由 .h 和 .c 文件组成的 JSON 解析器,由纯 C (ANSI C89) 实现,具有较好的跨平台性。cJSON 采用链表存储结构。

cJSON 库的使用非常简单,只需在项目中包含 cJSON.c 和 cJSON.h 即可。如果在命令行进行链接,则需要添加 -lm 表示链接 math 库。
在 Cgi 程序中,cJSON 库采用静态编译的方式。

cJSON 数据结构

cJSON 的类型定义如下:

#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_IsReference 256#define cJSON_StringIsConst 512
struct cJSON {    struct cJSON *next;    struct cJSON *prev;    struct cJSON *child;    int type;    char *valuestring;    int valueint;    double valuedouble;    char *string;};

节点数据结构为双向链表,其中 string 为结点的 key,child 指向数组或对象的子节点。

cJSON 常用方法

以下是 cJSON 的一些常用方法:

extern void cJSON_InitHooks(cJSON_Hooks* hooks);extern cJSON *cJSON_Parse(const char *value);extern char *cJSON_Print(const cJSON *item);extern char *cJSON_PrintUnformatted(const cJSON *item);extern char *cJSON_PrintBuffered(const cJSON *item, int prebuffer, int fmt);extern void cJSON_Delete(cJSON *c);extern int cJSON_GetArraySize(const cJSON *array);extern cJSON *cJSON_GetArrayItem(const cJSON *array, int item);extern cJSON *cJSON_GetObjectItem(const cJSON *object, const char *string);extern int cJSON_HasObjectItem(cJSON *object, const char *string);extern const char *cJSON_GetErrorPtr();

构造 JSON

以下是一些构造 JSON 的 API:

extern cJSON *cJSON_CreateNull(void);extern cJSON *cJSON_CreateTrue(void);extern cJSON *cJSON_CreateFalse(void);extern cJSON *cJSON_CreateBool(int b);extern cJSON *cJSON_CreateNumber(double num);extern cJSON *cJSON_CreateString(const char *string);extern cJSON *cJSON_CreateArray(void);extern cJSON *cJSON_CreateObject(void);

构造数组

以下是构造数组的 API:

extern cJSON *cJSON_CreateIntArray(const int *numbers, int count);extern cJSON *cJSON_CreateFloatArray(const float *numbers, int count);extern cJSON *cJSON_CreateDoubleArray(const double *numbers, int count);extern cJSON *cJSON_CreateStringArray(const char **strings, int count);

数组操作

以下是数组操作的 API:

extern void cJSON_AddItemToArray(cJSON *array, cJSON *item);extern void cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item);extern void cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item);extern void cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item);

判断函数

以下是一些判断函数:

CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item);CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item);CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item);CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item);CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item);CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item);CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item);CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item);CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item);CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item);

实例

1、解析 JSON
#include 
#include
#include "cJSON.h"char text[] = "{\"timestamp\":\"2019-03-03 08:45:57\", \"value\":1}";int main(int argc, const char *argv[]) { cJSON *json, *json_value, *json_timestamp; json = cJSON_Parse(text); if (json == NULL) { printf("Error before: [%s]\n", cJSON_GetErrorPtr()); return -1; } json_value = cJSON_GetObjectItem(json, "value"); if (json_value->type == cJSON_Number) { printf("value: %d\n", json_value->valueint); } json_timestamp = cJSON_GetObjectItem(json, "timestamp"); if (json_timestamp->type == cJSON_String) { printf("%s\n", json_timestamp->valuestring); } cJSON_Delete(json); return 0;}
2、创建 JSON
#include 
#include
#include "cJSON.h"int main(void) { char *cjson_str = NULL; cJSON *root = cJSON_CreateObject(); cJSON *item = cJSON_CreateObject(); cJSON *next = cJSON_CreateObject(); cJSON_AddItemToObject(root, "rc", cJSON_CreateNumber(0)); cJSON_AddItemToObject(root, "operation", cJSON_CreateString("CALL")); cJSON_AddItemToObject(root, "service", cJSON_CreateString("telephone")); cJSON_AddItemToObject(root, "text", cJSON_CreateString("打电话给张三")); cJSON_AddItemToObject(root, "semantic", item); cJSON_AddItemToObject(item, "slots", next); cJSON_AddItemToObject(next, "name", cJSON_CreateString("张三")); cJSON_AddStringToObject(next, "number", "13423452334"); cJSON_AddNumberToObject(next, "age", 35); cJSON_AddBoolToObject(next, "man", 1); cjson_str = cJSON_Print(root); printf("first json:\n%s\n", cjson_str); free(cjson_str); cJSON_Delete(root); return 0;}

转载地址:http://bjtwz.baihongyu.com/

你可能感兴趣的文章
MySQL 基础架构
查看>>
MySQL 基础模块的面试题总结
查看>>
MySQL 备份 Xtrabackup
查看>>
mYSQL 外键约束
查看>>
mysql 多个表关联查询查询时间长的问题
查看>>
mySQL 多个表求多个count
查看>>
mysql 多字段删除重复数据,保留最小id数据
查看>>
MySQL 多表联合查询:UNION 和 JOIN 分析
查看>>
MySQL 大数据量快速插入方法和语句优化
查看>>
mysql 如何给SQL添加索引
查看>>
mysql 字段区分大小写
查看>>
mysql 字段合并问题(group_concat)
查看>>
mysql 字段类型类型
查看>>
MySQL 字符串截取函数,字段截取,字符串截取
查看>>
MySQL 存储引擎
查看>>
mysql 存储过程 注入_mysql 视图 事务 存储过程 SQL注入
查看>>
MySQL 存储过程参数:in、out、inout
查看>>
mysql 存储过程每隔一段时间执行一次
查看>>
mysql 存在update不存在insert
查看>>
Mysql 学习总结(86)—— Mysql 的 JSON 数据类型正确使用姿势
查看>>