博客
关于我
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/

你可能感兴趣的文章
NSGA-Ⅲ源代码
查看>>
nsis 安装脚本示例(转)
查看>>
NSJSON的用法(oc系统自带的解析方法)
查看>>
nslookup 的基本知识与命令详解
查看>>
NSNumber与NSInteger的区别 -bei
查看>>
NSOperation基本操作
查看>>
NSRange 范围
查看>>
NSSet集合 无序的 不能重复的
查看>>
NSURLSession下载和断点续传
查看>>
NSUserdefault读书笔记
查看>>
NS图绘制工具推荐
查看>>
NT AUTHORITY\NETWORK SERVICE 权限问题
查看>>
NT symbols are incorrect, please fix symbols
查看>>
ntelliJ IDEA 报错:找不到包或者找不到符号
查看>>
NTFS文件权限管理实战
查看>>
ntko web firefox跨浏览器插件_深度比较:2019年6个最好的跨浏览器测试工具
查看>>
ntko文件存取错误_苹果推送 macOS 10.15.4:iCloud 云盘文件夹共享终于来了
查看>>
ntp server 用法小结
查看>>
ntpdate 通过外网同步时间
查看>>
ntpdate同步配置文件调整详解
查看>>