ImageVerifierCode 换一换
格式:DOCX , 页数:16 ,大小:60.92KB ,
资源ID:6749177      下载积分:2 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/6749177.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(JSONc语言开发指南.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

JSONc语言开发指南.docx

1、JSONc语言开发指南JSON c语言开发指南版本修改记录日期修订人1. 引言本文档是基于json-c 库对数据交换进行开发所编写的开发指南,及详细解释json-c库中常用api。 适用于开发人员使用c语言对json的编程。(注: 此文档json-c库版本为0.8json-c-0.8)2. JSON简介JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。易于人阅读和编写。同时也易于机器解析和生成。它基于JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 19

2、99的一个子集。 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),纪录(rec

3、ord),结构(struct),字典(dictionary),哈希表(hash table),有键列表(keyed list),或者关联数组 (associative array)。 值的有序列表(An ordered list of values)。在大部分语言中,它被理解为数组(array)。 这些都是常见的数据结构。事实上大部分现代计算机语言都以某种形式支持它们。这使得一种数据格式在同样基于这些结构的编程语言之间交换成为可能。2.2 JSON具有以下这些形式:对象是一个无序的“名称/值对”集合。一个对象以“”(左括号)开始,“”(右括号)结束。每个“名称”后跟一个“:”(冒号);“名称/

4、值 对”之间使用“,”(逗号)分隔。 数组是值(value)的有序集合。一个数组以“”(左中括号)开始,“”(右中括号)结束。值之间使用“,”(逗号)分隔。 值(value)可以是双引号括起来的字符串(string)、数值(number)、true、false、 null、对象(object)或者数组(array)。这些结构可以嵌套。 字符串(string)是由双引号包围的任意数量Unicode字符的集合,使用反斜线转义。一个字符(character)即一个单独的字符串(character string)。 字符串(string)与C或者Java的字符串非常相似。 数值(number)也与C或

5、者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_stringstruct json_object * );说明:创建个空的json_type_object类型JSON对象。struct json_object* boolean b);说明: 创建个js

6、on_type_boolean值类型json对象boolean struct json_object *obj);说明: 从json对象中boolean值类型得到boolean值同样: struct json_object* int i) int struct json_object *this) struct json_object* double d) double struct json_object *this) struct json_object* char *s) char* struct json_object *this)struct json_object * );说明:创

7、建个空的json_type_array类型JSON数组值对象。struct json_object * char *str);说明:由str里的JSON字符串生成JSON对象,str是son_object_to_) 生成的。参数: str json字符串struct json_object * struct json_object * json,char *name);说明:从json中按名字取一个对象。参数: json json对象 name - json域名字3.2 JSON对象的释放struct json_object * * struct json_object * this)说明:

8、增加对象引用计数。使用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的引用计数(调用函数a), 这时如果先释放b, 后释放c, 当释放b时, 并不会真正的释放a, 而是减少a的引用计数为1, 然后释放c时, 才真正释放a.参数: th

9、is json对象Void struct json_object * this)说明: 减少对象引用次数一次,当减少到0就释放(free)资源参数: this json对象样例片段: my_string = t);/*输出 my_string= */ t(table键) printf(my_string=%sn, my_string);/*转换json格式字符串 输出my_string.to_string()=t*/printf(my_string.to_string()=%sn, my_string); /*释放资源*/ my_string);3.3 JSON对象的操作Int struct

10、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_stringenum json_type json_object_get_type(struct json_object * this ) 说明: 得到json_object的类型。参数: this json对象

11、char * struct json_object * this)说明: 将json_object内容转换json格式字符串,其中可能含有转义符。参数: this json对象返回值: Json格式字符串void struct json_object* obj, char *key, struct json_object *val);说明: 添加个对象域到json对象中参数: Obj json对象 key 域名字 val json值对象void struct json_object* obj, char *key);说明: 删除key值json对象参数: ob j json对象 key 域名字

12、int struct json_object *obj);说明: 得到json对象数组的长度。参数: ob j json数组值对象extern int struct json_object *obj, struct json_object *val);说明: 添加一元素在json对象数组末端参数:ob j json数组值对象val json值对象*int struct json_object *obj, int idx, struct json_object *val);说明: 在指定的json对象数组下标插入或替换一个json对象元素。参数:ob j json数组值对象val json值对象

13、 idx 数组下标struct json_object * struct json_object * json_array,int i);说明:从数组中,按下标取JSON值对象。参数: json_array json 数组类型对象 i 数组下标位置定义宏 obj,key,val)说明: 遍历json对象的key和值 (key, val默认参数不变)样例片段: /*创建个空json对象值数组类型*/ my_array = ); /*添加json值类型到数组中*/ my_array, 1); my_array, 2); my_array, 3); my_array, 4, 5); printf(m

14、y_array=n); for(i=0; i my_array); i+) struct json_object *obj = my_array, i); printf(t%d=%sn, i, obj); printf(my_array.to_string()=%sn, my_array); my_object = ); /*添加json名称和值到json对象集合中*/ my_object, abc, 12); my_object, foo, bar); my_object, bool0, 0); my_object, bool1, 1); my_object, baz, bang); /*同

15、样的key 添加会替换掉*/ my_object, baz, fark); my_object, baz); /*添加数组集合到json对象中*/my_object, arr, my_array); printf(my_object=n); /*遍历json对象集合*/ my_object, key, val) printf(t%s: %sn, key, val); my_object);4. JSON实例开发4.1 样例1#include #include #include #include #include json.hint main(int argc, char *argv) stru

16、ct json_tokener *tok; struct json_object *my_string, *my_int, *my_object, *my_array; struct json_object *new_obj; int i; my_string = t);/*输出 my_string= */ printf(my_string=%sn, my_string);/*转换json格式字符串 输出my_string.to_string()=t*/printf(my_string.to_string()=%sn, my_string); /*释放资源*/ my_string); my_s

17、tring = ); printf(my_string=%sn, my_string); printf(my_string.to_string()=%sn, my_string); my_string); my_string = foo); printf(my_string=%sn, my_string); printf(my_string.to_string()=%sn, my_string); my_int = 9); printf(my_int=%dn, my_int); printf(my_int.to_string()=%sn, my_int); /*创建个空json对象值数组类型*

18、/ my_array = ); /*添加json值类型到数组中*/ my_array, 1); my_array, 2); my_array, 3); my_array, 4, 5); printf(my_array=n); for(i=0; i my_array); i+) struct json_object *obj = my_array, i); printf(t%d=%sn, i, obj); printf(my_array.to_string()=%sn, my_array); my_object = ); /*添加json名称和值到json对象集合中*/ my_object, a

19、bc, 12); my_object, foo, bar); my_object, bool0, 0); my_object, bool1, 1); my_object, baz, bang); /*同样的key 添加会替换掉*/ my_object, baz, fark); my_object, baz); printf(my_object=n); /*遍历json对象集合*/ my_object, key, val) printf(t%s: %sn, key, val); printf(my_object.to_string()=%sn, my_object); /*对些不规则的串做了些解

20、析测试*/ new_obj = 003); printf(new_obj.to_string()=%sn, new_obj); new_obj); new_obj = /* hello */foo); printf(new_obj.to_string()=%sn, new_obj); new_obj); new_obj = / hellonfoo); printf(new_obj.to_string()=%sn, new_obj); new_obj); new_obj = u0041u0042u0043); printf(new_obj.to_string()=%sn, new_obj); n

21、ew_obj); new_obj = null); printf(new_obj.to_string()=%sn, new_obj); new_obj); new_obj = True); printf(new_obj.to_string()=%sn, new_obj); new_obj); new_obj = 12); printf(new_obj.to_string()=%sn, new_obj); new_obj); new_obj = 12.3); /*得到json double类型 printf(new_obj.to_string()=%sn, new_obj); new_obj);

22、 new_obj = n); printf(new_obj.to_string()=%sn, new_obj); new_obj); new_obj = nabcn); printf(new_obj.to_string()=%sn, new_obj); new_obj); new_obj = null); printf(new_obj.to_string()=%sn, new_obj); new_obj); new_obj = ); printf(new_obj.to_string()=%sn, new_obj); new_obj); new_obj = false); printf(new_

23、obj.to_string()=%sn, new_obj); new_obj); new_obj = abc,null,def,12); printf(new_obj.to_string()=%sn, new_obj); new_obj); new_obj = ); printf(new_obj.to_string()=%sn, new_obj); new_obj); new_obj = foo: bar ); printf(new_obj.to_string()=%sn, new_obj); new_obj); new_obj = foo: bar, baz: null, bool0: tr

24、ue ); printf(new_obj.to_string()=%sn, new_obj); new_obj); new_obj = foo: null, foo ); printf(new_obj.to_string()=%sn, new_obj); new_obj); new_obj = abc: 12, foo: bar, bool0: false, bool1: true, arr: 1, 2, 3, null, 5 ); printf(new_obj.to_string()=%sn, new_obj); new_obj); new_obj = foo ); if(is_error(

25、new_obj) printf(got error as expectedn); new_obj = foo); if(is_error(new_obj) printf(got error as expectedn); new_obj = foo); if(is_error(new_obj) printf(got error as expectedn); /* test incremental parsing */ tok = ); new_obj = tok, foo, 6); if(is_error(new_obj) printf(got error as expectedn); new_

26、obj = tok, : bar, 8); if(is_error(new_obj) printf(got error as expectedn); new_obj = tok, :13, 6); printf(new_obj.to_string()=%sn, new_obj); new_obj); tok); my_string); my_int); my_object); my_array); /*如果前面没有添加到对象中, 必须显示释放, 如果添加到对象中,已经释放对象,则无需调用, 在这务必小心,否则很容易内存泄漏*/ return 0;输出结果:my_string=my_string

27、.to_string()=tmy_string=my_string.to_string()=my_string=foomy_string.to_string()=foomy_int=9my_int.to_string()=9my_array= 0=1 1=2 2=3 3=null 4=5my_array.to_string()= 1, 2, 3, null, 5 my_object= abc: 12 foo: bar bool0: false bool1: truemy_object.to_string()= abc: 12, foo: bar, bool0: false, bool1: tr

28、ue new_obj.to_string()=u0003new_obj.to_string()=foonew_obj.to_string()=foonew_obj.to_string()=ABCnew_obj.to_string()=nullnew_obj.to_string()=truenew_obj.to_string()=12new_obj.to_string()=12.300000new_obj.to_string()= n new_obj.to_string()= nabcn 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()= fo

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1