网上看到的一片关于json-c的文章。收藏一下,忘记了出处,尽请作者谅解。

JSON c语言开发指南

 

1.    引言

本文档是基于json-c 库对数据交换进行开发所编写的开发指南,及详细解释json-c库中常用api。 适用于开发人员使用c语言对json的编程。

(注:  此文档json-c库版本为0.8——json-c-0.8)

2.    JSON简介

JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。易于人阅读和编写。同时也易于机器解析和生成。它基于JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999的一个子集。 JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#, Java, JavaScript, Perl, Python等)。这些特性使JSON成为理想的数据交换语言。

跟XML相比,JSON的优势在于格式简洁短小,特别是在处理大量复杂数据的时候,这个优势便显得非常突出。从各浏览器的支持来看,JSON解决了因不同浏览器对XML DOM解析方式不同而引起的问题。

2.1 JSON建构于两种结构:

  • “名称/值”对的集合(A collection of name/value pairs)。不同的语言中,它被理解为对象(object),纪录(record),结构(struct),字典(dictionary),哈希表(hash table),有键列表(keyed list),或者关联数组 (associative array)。
  • 值的有序列表(An ordered list of values)。在大部分语言中,它被理解为数组(array)。

这些都是常见的数据结构。事实上大部分现代计算机语言都以某种形式支持它们。这使得一种数据格式在同样基于这些结构的编程语言之间交换成为可能。

2.2 JSON具有以下这些形式:

对象是一个无序的“‘名称/值’对”集合。一个对象以“{”(左括号)开始,“}”(右括号)结束。每个“名称”后跟一个“:”(冒号);“‘名称/值’ 对”之间使用“,”(逗号)分隔。

数组是值(value)的有序集合。一个数组以“[”(左中括号)开始,“]”(右中括号)结束。值之间使用“,”(逗号)分隔。

值(value)可以是双引号括起来的字符串(string)、数值(number)、truefalsenull、对象(object)或者数组(array)。这些结构可以嵌套。

字符串(string)是由双引号包围的任意数量Unicode字符的集合,使用反斜线转义。一个字符(character)即一个单独的字符串(character string)。

字符串(string)与C或者Java的字符串非常相似。

数值(number)也与C或者Java的数值非常相似。除去未曾使用的八进制与十六进制格式。除去一些编码细节。

3.    JSON 库函数说明

3.1 JSON对象的生成

Json对象的类型:

json_type_object, “名称/值”对的集合

Json对象的值类型

json_type_boolean,

json_type_double,

json_type_int,

json_type_array, “值”的集合

json_type_string

struct json_object * json_object_new_object();

说明:

创建个空的json_type_object类型JSON对象。

struct json_object* json_object_new_boolean(Boolean b);

说明:

创建个json_type_boolean值类型json对象

Boolean json_object_get_boolean(struct json_object *obj);

说明:

从json对象中boolean值类型得到boolean值

同样:

struct json_object* json_object_new_int(int i)

int json_object_get_int(struct json_object *this)

struct json_object* json_object_new_double(double d)

double json_object_get_double(struct json_object *this)

struct json_object* json_object_new_string(char *s)

char* json_object_get_string(struct json_object *this)

struct json_object * json_object_new_array();

说明:

创建个空的json_type_array类型JSON数组值对象。

struct json_object * json_tokener_parse(char *str);

说明:

由str里的JSON字符串生成JSON对象,str是json_object_to_json_string() 生成的。

参数:

str – json字符串

struct json_object * json_object_object_get(struct json_object * json,char *name);

说明:

从json中按名字取一个对象。

参数:

json – json对象

name -  json域名字

3.2 JSON对象的释放

struct json_object * * json_object_get(struct json_object * this)

说明:

增加对象引用计数。使用c库最关心的是内存谁来分配, 谁来释放. jsonc的内存管理方式, 是基于引用计数的内存树(链), 如果把一个struct json_object 对象a, add到另一个对象b上, 就不用显式的释放(json_object_put) a了, 相当于把a挂到了b的对象树上, 释放b的时候, 就会释放a. 当a既add到b上, 又add到对象c上时会导致a被释放两次(double free), 这时可以增加a的引用计数(调用函数json_object_get(a)), 这时如果先释放b, 后释放c, 当释放b时, 并不会真正的释放a, 而是减少a的引用计数为1, 然后释放c时, 才真正释放a.

参数:

this – json对象

Void json_object_put(struct json_object * this)

说明:

减少对象引用次数一次,当减少到0就释放(free)资源

参数:

this – json对象

样例片段:

my_string = json_object_new_string("\t");

/*输出 my_string=   */ \t(table键)

printf("my_string=%s\n", json_object_get_string(my_string));

/*转换json格式字符串 输出my_string.to_string()="\t"*/

printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));

/*释放资源*/

json_object_put(my_string);

3.3 JSON对象的操作

Int json_object_is_type(struct json_object * this, enum json_type type)

说明:

检查json_object是json的某个类型

参数:

this: json_object 实例

type: json_type_boolean,json_type_double, json_type_int, json_type_object, json_type_array, json_type_string

enum json_type json_object_get_type(struct json_object * this )

说明:

得到json_object的类型。

参数:

this – json对象

char * json_object_to_json_string(struct json_object * this)

说明:

将json_object内容转换json格式字符串,其中可能含有转义符。

参数:

this – json对象

返回值:

Json格式字符串

void json_object_object_add(struct json_object* obj, char *key, struct json_object *val);

说明:

添加个对象域到json对象中

参数:

Obj – json对象

key – 域名字

val – json值对象

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

说明:

删除key值json对象

参数:

ob j – json对象

key – 域名字

int json_object_array_length(struct json_object *obj);

说明:

得到json对象数组的长度。

参数:

ob j – json数组值对象

extern int json_object_array_add(struct json_object *obj,

struct json_object *val);

说明:

添加一元素在json对象数组末端

参数:

ob j – json数组值对象

val – json值对象

*

int json_object_array_put_idx(struct json_object *obj, int idx,

struct json_object *val);

说明:

在指定的json对象数组下标插入或替换一个json对象元素。

参数:

ob j – json数组值对象

val – json值对象

idx – 数组下标

struct json_object * json_object_array_get_idx(struct json_object * json_array,int i);

说明:

从数组中,按下标取JSON值对象。

参数:

json_array – json 数组类型对象

i – 数组下标位置

定义宏 json_object_object_foreach(obj,key,val)

说明:

遍历json对象的key和值 (key, val默认参数不变)

样例片段:

/*创建个空json对象值数组类型*/

my_array = json_object_new_array();

/*添加json值类型到数组中*/

json_object_array_add(my_array, json_object_new_int(1));

json_object_array_add(my_array, json_object_new_int(2));

json_object_array_add(my_array, json_object_new_int(3));

json_object_array_put_idx(my_array, 4, json_object_new_int(5));

printf("my_array=\n");

for(i=0; i < json_object_array_length(my_array); i++) {

struct json_object *obj = json_object_array_get_idx(my_array, i);

printf("\t[%d]=%s\n", i, json_object_to_json_string(obj));

}

printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array));

my_object = json_object_new_object();

/*添加json名称和值到json对象集合中*/

json_object_object_add(my_object, "abc", json_object_new_int(12));

json_object_object_add(my_object, "foo", json_object_new_string("bar"));

json_object_object_add(my_object, "bool0", json_object_new_boolean(0));

json_object_object_add(my_object, "bool1", json_object_new_boolean(1));

json_object_object_add(my_object, "baz", json_object_new_string("bang"));

/*同样的key 添加会替换掉*/

json_object_object_add(my_object, "baz", json_object_new_string("fark"));

json_object_object_del(my_object, "baz");

/*添加数组集合到json对象中*/

json_object_object_add(my_object, "arr", my_array);

printf("my_object=\n");

/*遍历json对象集合*/

json_object_object_foreach(my_object, key, val) {

printf("\t%s: %s\n", key, json_object_to_json_string(val));

}

json_object_put(my_object);

4.    JSON实例开发

4.1 样例1

#include <stdio.h>

#include <stdlib.h>

#include <stddef.h>

#include <string.h>

#include "json.h"

int main(int argc, char **argv)

{

struct json_tokener *tok;

struct json_object *my_string, *my_int, *my_object, *my_array;

struct json_object *new_obj;

int i;

my_string = json_object_new_string("\t");

/*输出 my_string=   */

printf("my_string=%s\n", json_object_get_string(my_string));

/*转换json格式字符串 输出my_string.to_string()="\t"*/

printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));

/*释放资源*/

json_object_put(my_string);

my_string = json_object_new_string("\\");

printf("my_string=%s\n", json_object_get_string(my_string));

printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));

json_object_put(my_string);

my_string = json_object_new_string("foo");

printf("my_string=%s\n", json_object_get_string(my_string));

printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));

my_int = json_object_new_int(9);

printf("my_int=%d\n", json_object_get_int(my_int));

printf("my_int.to_string()=%s\n", json_object_to_json_string(my_int));

/*创建个空json对象值数组类型*/

my_array = json_object_new_array();

/*添加json值类型到数组中*/

json_object_array_add(my_array, json_object_new_int(1));

json_object_array_add(my_array, json_object_new_int(2));

json_object_array_add(my_array, json_object_new_int(3));

json_object_array_put_idx(my_array, 4, json_object_new_int(5));

printf("my_array=\n");

for(i=0; i < json_object_array_length(my_array); i++) {

struct json_object *obj = json_object_array_get_idx(my_array, i);

printf("\t[%d]=%s\n", i, json_object_to_json_string(obj));

}

printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array));

my_object = json_object_new_object();

/*添加json名称和值到json对象集合中*/

json_object_object_add(my_object, "abc", json_object_new_int(12));

json_object_object_add(my_object, "foo", json_object_new_string("bar"));

json_object_object_add(my_object, "bool0", json_object_new_boolean(0));

json_object_object_add(my_object, "bool1", json_object_new_boolean(1));

json_object_object_add(my_object, "baz", json_object_new_string("bang"));

/*同样的key 添加会替换掉*/

json_object_object_add(my_object, "baz", json_object_new_string("fark"));

json_object_object_del(my_object, "baz");

printf("my_object=\n");

/*遍历json对象集合*/

json_object_object_foreach(my_object, key, val) {

printf("\t%s: %s\n", key, json_object_to_json_string(val));

}

printf("my_object.to_string()=%s\n", json_object_to_json_string(my_object));

/*对些不规则的串做了些解析测试*/

new_obj = json_tokener_parse("\"\003\"");

printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));

json_object_put(new_obj);

new_obj = json_tokener_parse("/* hello */\"foo\"");

printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));

json_object_put(new_obj);

new_obj = json_tokener_parse("// hello\n\"foo\"");

printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));

json_object_put(new_obj);

new_obj = json_tokener_parse("\"\\u0041\\u0042\\u0043\"");

printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));

json_object_put(new_obj);

new_obj = json_tokener_parse("null");

printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));

json_object_put(new_obj);

new_obj = json_tokener_parse("True");

printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));

json_object_put(new_obj);

new_obj = json_tokener_parse("12");

printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));

json_object_put(new_obj);

new_obj = json_tokener_parse("12.3");

/*得到json double类型

printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));

json_object_put(new_obj);

new_obj = json_tokener_parse("[\"\\n\"]");

printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));

json_object_put(new_obj);

new_obj = json_tokener_parse("[\"\\nabc\\n\"]");

printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));

json_object_put(new_obj);

new_obj = json_tokener_parse("[null]");

printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));

json_object_put(new_obj);

new_obj = json_tokener_parse("[]");

printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));

json_object_put(new_obj);

new_obj = json_tokener_parse("[false]");

printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));

json_object_put(new_obj);

new_obj = json_tokener_parse("[\"abc\",null,\"def\",12]");

printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));

json_object_put(new_obj);

new_obj = json_tokener_parse("{}");

printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));

json_object_put(new_obj);

new_obj = json_tokener_parse("{ \"foo\": \"bar\" }");

printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));

json_object_put(new_obj);

new_obj = json_tokener_parse("{ \"foo\": \"bar\", \"baz\": null, \"bool0\": true }");

printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));

json_object_put(new_obj);

new_obj = json_tokener_parse("{ \"foo\": [null, \"foo\"] }");

printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));

json_object_put(new_obj);

new_obj = json_tokener_parse("{ \"abc\": 12, \"foo\": \"bar\", \"bool0\": false, \"bool1\": true, \"arr\": [ 1, 2, 3, null, 5 ] }");

printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));

json_object_put(new_obj);

new_obj = json_tokener_parse("{ foo }");

if(is_error(new_obj)) printf("got error as expected\n");

new_obj = json_tokener_parse("foo");

if(is_error(new_obj)) printf("got error as expected\n");

new_obj = json_tokener_parse("{ \"foo");

if(is_error(new_obj)) printf("got error as expected\n");

/* test incremental parsing */

tok = json_tokener_new();

new_obj = json_tokener_parse_ex(tok, "{ \"foo", 6);

if(is_error(new_obj)) printf("got error as expected\n");

new_obj = json_tokener_parse_ex(tok, "\": {\"bar", 8);

if(is_error(new_obj)) printf("got error as expected\n");

new_obj = json_tokener_parse_ex(tok, "\":13}}", 6);

printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));

json_object_put(new_obj);

json_tokener_free(tok);

json_object_put(my_string);

json_object_put(my_int);

json_object_put(my_object);

json_object_put(my_array);

/*如果前面没有添加到对象中, 必须显示释放,

如果添加到对象中,已经释放对象,则无需调用, 在这务必小心,否则很容易内存泄漏*/

return 0;

}

输出结果:

my_string=

my_string.to_string()="\t"

my_string=\

my_string.to_string()="\\"

my_string=foo

my_string.to_string()="foo"

my_int=9

my_int.to_string()=9

my_array=

[0]=1

[1]=2

[2]=3

[3]=null

[4]=5

my_array.to_string()=[ 1, 2, 3, null, 5 ]

my_object=

abc: 12

foo: "bar"

bool0: false

bool1: true

my_object.to_string()={ "abc": 12, "foo": "bar", "bool0": false, "bool1": true }

new_obj.to_string()="\u0003"

new_obj.to_string()="foo"

new_obj.to_string()="foo"

new_obj.to_string()="ABC"

new_obj.to_string()=null

new_obj.to_string()=true

new_obj.to_string()=12

new_obj.to_string()=12.300000

new_obj.to_string()=[ "\n" ]

new_obj.to_string()=[ "\nabc\n" ]

new_obj.to_string()=[ null ]

new_obj.to_string()=[ ]

new_obj.to_string()=[ false ]

new_obj.to_string()=[ "abc", null, "def", 12 ]

new_obj.to_string()={ }

new_obj.to_string()={ "foo": "bar" }

new_obj.to_string()={ "foo": "bar", "baz": null, "bool0": true }

new_obj.to_string()={ "foo": [ null, "foo" ] }

new_obj.to_string()={ "abc": 12, "foo": "bar", "bool0": false, "bool1": true, "arr": [ 1, 2, 3, null, 5 ] }

got error as expected

got error as expected

got error as expected

new_obj.to_string()={ "foo": { "bar": 13 } }

备注

Json-c-0.8版本对0.7版本做的更新:

* 添加va_end 给指针至NULL 增加程序健壮性

* 添加宏使得能够编译出调试代码

* 解决个bug 在指数中使用大写字母E

* 添加 stddef.h 头文件

* 允许编译json-c 使用-Werror

json-c开发指南的更多相关文章

  1. Knockout应用开发指南 第六章:加载或保存JSON数据

    原文:Knockout应用开发指南 第六章:加载或保存JSON数据 加载或保存JSON数据 Knockout可以实现很复杂的客户端交互,但是几乎所有的web应用程序都要和服务器端交换数据(至少为了本地 ...

  2. JVM 平台上的各种语言的开发指南

    JVM 平台上的各种语言的开发指南 为什么我们需要如此多的JVM语言? 在2013年你可以有50中JVM语言的选择来用于你的下一个项目.尽管你可以说出一大打的名字,你会准备为你的下一个项目选择一种新的 ...

  3. nodejs开发指南读后感

    nodejs开发指南读后感 阅读目录 使用nodejs创建http服务器; supervisor的使用及nodejs常见的调式代码命令了解; 了解Node核心模块; ejs模板引擎 Express 理 ...

  4. jQuery MiniUI 开发指南+API组件参考手册

    jQuery MiniUI 开发指南 本文档将逐步的讲解jQuery MiniUI的方方面面,从此您将踏上jQuery MiniUI的深入探索之旅.                 1.Hello M ...

  5. ECSHOP二次开发指南

    ECSHOP二次开发指南 发布时间:2013-05-28 12:47:00   来源:   评论:0 点击: 次 [字号:大 中 小] QQ空间新浪微博腾讯微博人人网豆瓣网百度空间百度搜藏开心网复制更 ...

  6. curl网站开发指南

    我一向以为,curl只是一个编程用的函数库. 最近才发现,这个命令本身,就是一个无比有用的网站开发工具,请看我整理的它的用法. =================================== ...

  7. Knockout应用开发指南(完整版) 目录索引

    http://learn.knockoutjs.com/  所有示例和代码都在在上面直接运行预览 注意:因为它用了google的cdn加速,所要要用代_理+_翻_墙才能正常加载 使用Knockout有 ...

  8. Knockout应用开发指南 第九章:高级应用举例

    原文:Knockout应用开发指南 第九章:高级应用举例 1   Contacts editor 这个例子和微软为演示jQuery Data Linking Proposal例子提供的例子一样的提供的 ...

  9. Knockout应用开发指南 第七章:Mapping插件

    原文:Knockout应用开发指南 第七章:Mapping插件 Mapping插件 Knockout设计成允许你使用任何JavaScript对象作为view model.必须view model的一些 ...

  10. Knockout应用开发指南 第三章:绑定语法(1)

    原文:Knockout应用开发指南 第三章:绑定语法(1) 第三章所有代码都需要启用KO的ko.applyBindings(viewModel);功能,才能使代码生效,为了节约篇幅,所有例子均省略了此 ...

随机推荐

  1. Android-图像原理/绘制原理

    图像原理 先专门讲解图片,一想到图片有那些特点:    宽/高 格式jpg/jpeg/... 大小43.kb/... 图片是由像素点组成:像素点是正方形的,只是像素点很小很多,看起来可以拼接圆形⭕️, ...

  2. Provider 模式

    Provider 模式:为一个API进行定义和实现的分离. 常见场景:DBPrider切换,第3方集成API切换 以发邮件为例: Email Provider Config: public abstr ...

  3. ADB server didn't ACK问题,连上手机问题(转)

    出现如下情况 ADB server didn't ACK* failed to start daemon * 解决办法: 方法一: (1)查看任务管理器,关闭所有adb.exe,或者运行->cm ...

  4. leetcode 验证回文串

    给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, a plan, a c ...

  5. django系列6--Ajax01 特点, 基本格式, 向前端发送数据

    一.Ajax了解 AJAX(Asynchronous Javascript And XML)优点是在不重新加载整个页面的情况下,可以与服务器交换数据并更新部分网页内容 优点: 1.ajax使用Java ...

  6. 73. 矩阵置零 leetcode JAVA

    题目: 给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0.请使用原地算法. 示例 1: 输入: [   [1,1,1],   [1,0,1],   [1,1,1] ...

  7. 使用Fiddler代理调试本地手机页面

    从事前端开发的同学一定对 Fiddler 不陌生,它是一个非常强大的http(s)协议分析工具.我们知道如何在电脑上调试页面请求,但在手机端你没有这么多强大好用的调试工具来调试你的webapp,如果你 ...

  8. django入门-模型-part2

    尊重作者的劳动,转载请注明作者及原文地址 http://www.cnblogs.com/txwsqk/p/6511177.html 完全翻译自官方文档 https://docs.djangoproje ...

  9. Laravel 的核心概念

    工欲善其事,必先利其器.在开发Xblog的过程中,稍微领悟了一点Laravel的思想.确实如此,这篇文章读完你可能并不能从无到有写出一个博客,但知道Laravel的核心概念之后,当你再次写起Larav ...

  10. Optional类

    参照: 一篇简单使用介绍 官网详细用法介绍 包含各种例子的cheetsheet 一个封装某个value的容器 一般可用于方法返回值类型,提醒调用方,这个值可能为null,所以需要处理(因为空指针异常是 ...