博客
关于我
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中的using
查看>>
MySQL中的关键字深入比较:UNION vs UNION ALL
查看>>
mysql中的四大运算符种类汇总20多项,用了三天三夜来整理的,还不赶快收藏
查看>>
mysql中的字段如何选择合适的数据类型呢?
查看>>
MySQL中的字符集陷阱:为何避免使用UTF-8
查看>>
mysql中的数据导入与导出
查看>>
MySQL中的时间函数
查看>>
mysql中的约束
查看>>
MySQL中的表是什么?
查看>>
mysql中穿件函数时候delimiter的用法
查看>>
Mysql中索引的分类、增删改查与存储引擎对应关系
查看>>
Mysql中索引的最左前缀原则图文剖析(全)
查看>>
MySql中给视图添加注释怎么添加_默认不支持_可以这样取巧---MySql工作笔记002
查看>>
Mysql中获取所有表名以及表名带时间字符串使用BetweenAnd筛选区间范围
查看>>
Mysql中视图的使用以及常见运算符的使用示例和优先级
查看>>
Mysql中触发器的使用示例
查看>>
Mysql中设置只允许指定ip能连接访问(可视化工具的方式)
查看>>
mysql中还有窗口函数?这是什么东西?
查看>>
mysql中间件
查看>>
MYSQL中频繁的乱码问题终极解决
查看>>