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

本文共 4969 字,大约阅读时间需要 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/

你可能感兴趣的文章
Nginx的使用总结(一)
查看>>
Nginx的使用总结(三)
查看>>
Nginx的使用总结(二)
查看>>
Nginx的可视化神器nginx-gui的下载配置和使用
查看>>
Nginx的是什么?干什么用的?
查看>>
Nginx访问控制_登陆权限的控制(http_auth_basic_module)
查看>>
nginx负载均衡和反相代理的配置
查看>>
nginx负载均衡器处理session共享的几种方法(转)
查看>>
nginx负载均衡的5种策略(转载)
查看>>
nginx负载均衡的五种算法
查看>>
nginx转发端口时与导致websocket不生效
查看>>
Nginx运维与实战(二)-Https配置
查看>>
Nginx配置Https证书
查看>>
Nginx配置ssl实现https
查看>>
Nginx配置TCP代理指南
查看>>
Nginx配置——不记录指定文件类型日志
查看>>
nginx配置一、二级域名、多域名对应(api接口、前端网站、后台管理网站)
查看>>
Nginx配置代理解决本地html进行ajax请求接口跨域问题
查看>>
nginx配置全解
查看>>
Nginx配置参数中文说明
查看>>