JSON c语言开发指南Word格式.docx

上传人:b****5 文档编号:8486259 上传时间:2023-05-11 格式:DOCX 页数:16 大小:60.92KB
下载 相关 举报
JSON c语言开发指南Word格式.docx_第1页
第1页 / 共16页
JSON c语言开发指南Word格式.docx_第2页
第2页 / 共16页
JSON c语言开发指南Word格式.docx_第3页
第3页 / 共16页
JSON c语言开发指南Word格式.docx_第4页
第4页 / 共16页
JSON c语言开发指南Word格式.docx_第5页
第5页 / 共16页
JSON c语言开发指南Word格式.docx_第6页
第6页 / 共16页
JSON c语言开发指南Word格式.docx_第7页
第7页 / 共16页
JSON c语言开发指南Word格式.docx_第8页
第8页 / 共16页
JSON c语言开发指南Word格式.docx_第9页
第9页 / 共16页
JSON c语言开发指南Word格式.docx_第10页
第10页 / 共16页
JSON c语言开发指南Word格式.docx_第11页
第11页 / 共16页
JSON c语言开发指南Word格式.docx_第12页
第12页 / 共16页
JSON c语言开发指南Word格式.docx_第13页
第13页 / 共16页
JSON c语言开发指南Word格式.docx_第14页
第14页 / 共16页
JSON c语言开发指南Word格式.docx_第15页
第15页 / 共16页
JSON c语言开发指南Word格式.docx_第16页
第16页 / 共16页
亲,该文档总共16页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

JSON c语言开发指南Word格式.docx

《JSON c语言开发指南Word格式.docx》由会员分享,可在线阅读,更多相关《JSON c语言开发指南Word格式.docx(16页珍藏版)》请在冰点文库上搜索。

JSON c语言开发指南Word格式.docx

一个对象以“{”(左括号)开始,“}”(右括号)结束。

每个“名称”后跟一个“:

”(冒号);

“‘名称/值’对”之间使用“,”(逗号)分隔。

数组是值(value)的有序集合。

一个数组以“[”(左中括号)开始,“]”(右中括号)结束。

值之间使用“,”(逗号)分隔。

值(value)可以是双引号括起来的字符串(string)、数值(number)、true、false、null、对象(object)或者数组(array)。

这些结构可以嵌套。

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

一个字符(character)即一个单独的字符串(characterstring)。

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

数值(number)也与C或者Java的数值非常相似。

除去未曾使用的八进制与十六进制格式。

除去一些编码细节。

3.JSON库函数说明

3.1JSON对象的生成

Json对象的类型:

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

Json对象的值类型

json_type_boolean,

json_type_double,

json_type_int,

json_type_array,“值”的集合

json_type_string

structjson_object*);

说明:

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

structjson_object*booleanb);

创建个json_type_boolean值类型json对象

booleanstructjson_object*obj);

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

同样:

structjson_object*inti)

intstructjson_object*this)

structjson_object*doubled)

doublestructjson_object*this)

structjson_object*char*s)

char*structjson_object*this)

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

structjson_object*char*str);

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

参数:

str–json字符串

structjson_object*structjson_object*json,char*name);

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

json–json对象

name-json域名字

3.2JSON对象的释放

structjson_object**structjson_object*this)

增加对象引用计数。

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

this–json对象

Voidstructjson_object*this)

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

样例片段:

my_string="

\t"

);

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

printf("

my_string=%s\n"

my_string));

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

*/

printf("

my_string.to_string()=%s\n"

/*释放资源*/

my_string);

3.3JSON对象的操作

Intstructjson_object*this,enumjson_typetype)

检查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

enumjson_typejson_object_get_type(structjson_object*this)

得到json_object的类型。

char*structjson_object*this)

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

返回值:

Json格式字符串

voidstructjson_object*obj,char*key,structjson_object*val);

添加个对象域到json对象中

Obj–json对象

key–域名字

val–json值对象

voidstructjson_object*obj,char*key);

删除key值json对象

obj–json对象

intstructjson_object*obj);

得到json对象数组的长度。

obj–json数组值对象

externintstructjson_object*obj,

structjson_object*val);

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

obj–json数组值对象

val–json值对象

*

intstructjson_object*obj,intidx,

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

idx–数组下标

structjson_object*structjson_object*json_array,inti);

从数组中,按下标取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));

my_array=\n"

for(i=0;

i<

my_array);

i++){

structjson_object*obj=my_array,i);

\t[%d]=%s\n"

i,obj));

}

my_array.to_string()=%s\n"

my_array));

my_object=);

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

my_object,"

abc"

12));

foo"

"

bar"

));

bool0"

0));

bool1"

1));

baz"

bang"

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

fark"

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

my_object,"

arr"

my_array);

my_object=\n"

/*遍历json对象集合*/

my_object,key,val){

\t%s:

%s\n"

key,val));

my_object);

4.JSON实例开发

4.1样例1

#include<

stdio.h>

stdlib.h>

stddef.h>

string.h>

#include"

json.h"

intmain(intargc,char**argv)

{

structjson_tokener*tok;

structjson_object*my_string,*my_int,*my_object,*my_array;

structjson_object*new_obj;

inti;

/*输出my_string=*/

\\"

my_int=9);

my_int=%d\n"

my_int));

my_int.to_string()=%s\n"

my_object.to_string()=%s\n"

my_object));

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

new_obj="

\"

\003\"

"

new_obj.to_string()=%s\n"

new_obj));

new_obj);

/*hello*/\"

foo\"

//hello\n\"

\\u0041\\u0042\\u0043\"

null"

True"

12"

12.3"

/*得到jsondouble类型

[\"

\\n\"

]"

\\nabc\\n\"

[null]"

[]"

[false]"

abc\"

null,\"

def\"

12]"

{}"

{\"

:

\"

bar\"

}"

\"

baz\"

null,\"

bool0\"

true}"

[null,\"

]}"

12,\"

false,\"

bool1\"

true,\"

arr\"

[1,2,3,null,5]}"

{foo}"

if(is_error(new_obj))printf("

goterrorasexpected\n"

/*testincrementalparsing*/

tok=);

new_obj=tok,"

6);

{\"

8);

13}}"

tok);

my_int);

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

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

return0;

}

输出结果:

my_string=

my_string.to_string()="

my_string=\

my_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:

"

bool0:

false

bool1:

true

my_object.to_string()={"

12,"

false,"

true}

new_obj.to_string()="

\u0003"

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"

]

\nabc\n"

new_obj.to_string()=[null]

new_obj.to_string()=[]

new_obj.to_string()=[false]

null,"

def"

12]

new_obj.to_string()={}

new_obj.to_string()={"

f

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 幼儿教育 > 幼儿读物

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2