JSON解析器json-c

一、介绍

JSON-C实现了一个引用计数对象模型,它允许您轻松地使用C语言来构建JSON对象,将它们输出为JSON格式的字符串,并将JSON格式字符串解析回JSON对象的C语言表示形式。它的目标是符合RFC 7159标准。

二、编译

2.1 automake

使用automake的编译过程如下:

$ git clone https://github.com/json-c/json-c.git
$ cd json-c
$ sh autogen.sh
// 标准的三部曲
$ ./configure  # --enable-threading
$ make
$ make install
// 编译并运行测试程序
$ make check
$ make USE_VALGRIND=0 check   # optionally skip using valgrind

2.2 cmake

使用cmake编译的过程如下:

mkdir build
cd build
cmake ../
make

cmake可选的几个编译选项为:

Variable Type Description
CMAKE_INSTALL_PREFIX String The install location.
BUILD_SHARED_LIBS Bool The default build generates a dynamic (dll/so) library. Set this to OFF to create a static library instead.
ENABLE_RDRAND Bool Enable RDRAND Hardware RNG Hash Seed
ENABLE_THREADING Bool Enable partial threading support

三、使用

3.1 头文件

要使用json-c,最简单的方式是包含json.h头文件即可,或者最好是下列更具体的头文件之一:

  • json_object.h:核心类型和方法;
  • json_tokener.h:用于解析和序列化json-c对象树的方法;
  • json_pointer.h:用于从JSON-c对象树中检索对象的JSON指针(RFC 6901)实现;
  • json_object_iterator.h:用于迭代单个json_object实例的方法;
  • json_visit.h:遍历json-c对象树的方法;
  • json_util.h:其他混杂工具集函数。

3.2 API介绍

详细且全面的API介绍文档:http://json-c.github.io/json-c/

3.2.1 JSON对象类型

JSON-C支持的JSON对象类型有7种:

typedef enum json_type {
  /* If you change this, be sure to update json_type_to_name() too */
  json_type_null,
  json_type_boolean,
  json_type_double,
  json_type_int,
  json_type_object,
  json_type_array,
  json_type_string
} json_type;

3.2.2 创建JSON对象

下面系列函数用于创建一个JSON对象:

struct json_object *    json_object_new_object (void);
struct json_object *    json_object_new_array (void);
struct json_object *    json_object_new_boolean (json_bool b);
struct json_object *    json_object_new_int (int32_t i);
struct json_object *    json_object_new_int64 (int64_t i);
struct json_object *    json_object_new_double (double d);
struct json_object *    json_object_new_double_s (double d, const char *ds);
struct json_object *    json_object_new_string (const char *s);
struct json_object *    json_object_new_string_len (const char *s, int len);
  • json_object_new_object()创建一个新的json对象,引用计数为1,该指针具有唯一的所有权;当使用json_object_object_add()或者json_object_array_put_idx()作用于该对象时,所有权转移到另一方。使用json_object_get作用于该对象的后,必须使用json_object_put释放。
  • json_object_new_array()创建一个JSON数组类型JSON对象;
  • json_object_new_boolean()创建一个布尔类型JSON对象;
  • json_object_new_int()创建32位整形JSON对象;
  • json_object_new_int64()创建64位整形JSON对象;
  • json_object_new_double()创建double类型JSON对象;
  • json_object_new_string()将C字符串转换为JSON字符串格式的对象;

3.2.3 增加/删除/修改

给JSON对象增加字段(不会增加引用计数):

int json_object_object_add (struct json_object *obj, const char *key, struct json_object *val);
int json_object_object_add_ex (struct json_object *obj, const char *const key, struct json_object *const val, const unsigned opts);

删除json对象的指定字段,被删除的对象引用计数减去1,如果这个val没有更多的所有者,这个key对应的val被free,否则这个val的引用保存在内存中:

void json_object_object_del (struct json_object *obj, const char *key);

增加一个元素到json数组的末尾,obj引用计数不会增加,增加字段的方式更加紧凑;如果需要获取val的引用,需要用json_object_get()来传递该对象:

int json_object_array_add (struct json_object *obj, struct json_object *val);

替换json数组中的值:

int json_object_array_put_idx (struct json_object *obj, size_t idx, struct json_object *val);

json数组的排序,这里需要自己写排序函数:

void json_object_array_sort (struct json_object *jso, int(*sort_fn)(const void *, const void *));

3.2.4 取值

获取json对象的长度,依据字段的数目:

int json_object_object_length (const struct json_object *obj);

获取json对象的哈希表:

struct lh_table * json_object_get_object (const struct json_object *obj);

获取对象的数组列表:

struct array_list * json_object_get_array (const struct json_object *obj);

获取json的类型:

enum json_type json_object_get_type (const struct json_object *obj);

获取json数组对象的长度:

size_t json_object_array_length (const struct json_object *obj);

获取json对象的bool值,int和double对象是0转换为FALSE,否则返回TRUE;非0长度的字符串返回TRUE;其他对象非空的话,返回TRUE:

json_bool json_object_get_boolean (const struct json_object *obj);

获取json对象的长度,如果参数不是string类型的json,返回0:

int json_object_get_string_len (const struct json_object *obj);

按照索引获取json数组的对象:

struct json_object * json_object_array_get_idx (const struct json_object *obj, size_t idx);

3.2.5 类型转换

转换json对象到c字符串格式:

const char * json_object_to_json_string (struct json_object *obj);

获取JSON中指定类型的数值:

json_bool json_object_get_boolean (const struct json_object *obj);
int32_t json_object_get_int (const struct json_object *obj);
int64_t json_object_get_int64 (const struct json_object *obj);
double json_object_get_double (const struct json_object *obj);
const char * json_object_get_string (struct json_object *obj);

将字符串转换为json对象:

struct json_object * json_tokener_parse (const char *str)

3.2.6 JSON对象释放

以下两个函数配合使用,前者获取该对象指针的所有权,引用计数加1,如果对象已经被释放,返回NULL;后者引用计数减1,如果对象已经被释放,返回1:

json_object * json_object_get (struct json_object *obj);
int json_object_put (struct json_object *obj);

3.2.7 其他方法

类型判断:

int json_object_is_type (const struct json_object *obj, enum json_type type);

3.3 json_util.h

json_util.h提供了有关文件读写操作的函数,这个文件的内容是json格式的:

/* utility functions */
extern struct json_object* json_object_from_file(const char *filename);
extern struct json_object* json_object_from_fd(int fd);
extern int json_object_to_file(const char *filename, struct json_object *obj);
extern int json_object_to_file_ext(const char *filename, struct json_object *obj, int flags);
extern int json_object_to_fd(int fd, struct json_object *obj, int flags);
const char *json_util_get_last_err(void);
extern int json_parse_int64(const char *buf, int64_t *retval);
extern int json_parse_double(const char *buf, double *retval);
extern const char *json_type_to_name(enum json_type o_type);
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,569评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,499评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,271评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,087评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,474评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,670评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,911评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,636评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,397评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,607评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,093评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,418评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,074评论 3 237
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,092评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,865评论 0 196
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,726评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,627评论 2 270

推荐阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,036评论 1 32
  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML标准。 注意:讲述HT...
    kismetajun阅读 27,111评论 1 45
  • 概要 64学时 3.5学分 章节安排 电子商务网站概况 HTML5+CSS3 JavaScript Node 电子...
    阿啊阿吖丁阅读 8,627评论 0 3
  • 1.设计模式是什么? 你知道哪些设计模式,并简要叙述?设计模式是一种编码经验,就是用比较成熟的逻辑去处理某一种类型...
    龍飝阅读 2,057评论 0 12
  • 你是我曾跨跃的山峰, 也是将我淹没的河流, 你是我梦中的蝴蝶 也曾是我迷离的梦幻 你是我开启幸福的动力 也是埋葬我...
    麦积梦境阅读 391评论 7 3