博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
json-c API总结
阅读量:4005 次
发布时间:2019-05-24

本文共 4468 字,大约阅读时间需要 14 分钟。

分类: 
 
572人阅读 
(0) 
 

最新版本: 

编译假如出错时:忽略指定库libcmt.lib  libcmdtd.lib(debug)  在哪加不用我说吧

解析的时候有一点点小问题 :比如 http://baidu.com   解出来后是http:\/baidu.com ,需修改原代码

  json_object.c 中  json_escape_st函数大约第20行的位置 

 else if(c == '/') printbuf_memappend(pb, "\\/", 2); 

 改为else if(c == '/') printbuf_memappend(pb, "/", 1);

我是这么做的,成功!

json_object的格式:(注意:一个json对象内可以包含多个json对象)

{'latitude':116.40091896057129,'longitude':39.931129903495886}

json_object数组的格式:

"[{'latitude':116.40091896057129,'longitude':39.931129903495886},

{'latitude':116.40194892883301,'longitude':39.946134395563796},
{'latitude':116.39645576477051,'longitude':39.95488549657055}]"

序言:json提供的方法就是处理:

基础数据类型(在json中冶同为json对象):int,bool,float,double等等,

json类型:与{'latitude':116.40091896057129,'longitude':39.931129903495886}(这里面包含了2个json对象,而每个json对象{'latitude':116.40091896057129}中又包含了一个json对象:39.95488549657055)

json数组

一,json_object.h:创建Json对象:json_object_new 开头

A,创建一个Json对象:

struct json_object * json_object_new_object (void)


B,创建一个Json数组对象:

struct json_object * json_object_new_array (void)

二,json_tokener.h:将json格式的string转化为json对象的方法:失败返回null

1.struct* (const char *str)

//例如:json_object *req_json = json_tokener_parse( req_buf->buf );

三,json_object.h:将json对象转化为json格式的string:失败返回null

const char * json_object_to_json_string (struct json_object *obj)

//将json_object转化为json格式的string,这个方法与后面的json_object_get_string不同,大家知道json对象的结构为:key:test value :haha ;那么to_json_string对象后,结构为:='latitude':116.40091896057129,'longitude':39.931129903495886

而json_object_get_string仅仅负责对单纯"test"的json对象转化,例如:

json_object * j_o = json_object_new_string("test");

char * pointer_char = json_object_get_string(j_o); //结果pointer_char 为"test"

四,json_object.h:向json_object内的增删查:无改操作

添加:void json_object_object_add (struct json_object *obj, const char *key, struct json_object *val)
//例如:json_object_object_add(item, "id", json_object_new_int(_id));

查询:struct json_object * json_object_object_get (struct json_object *obj, const char *key)

删除:void json_object_object_del (struct json_object *obj, const char *key)

五,json_object.h:将其他基础数据类型转化为json基础类型的方法:

1,struct json_object * json_object_new_int (int i)


2,struct json_object * json_object_new_double (double d)

3,struct json_object * json_object_new_string (const char *s)


4,struct json_object * json_object_new_boolean (boolean b)


5,struct json_object * json_object_new_string_len (const char *s, int len)
(获得s的部分字符串,并转换为json对象)


六,json_object.h:将基础数据类型的Json对象转化为另外一个json对象以及转化为其他基础数据类型:json_object_get

1,struct json_object * json_object_get (struct json_object *obj)
//将json对象转化为另外一个json对象

2.struct lh_table * json_object_get_object (struct json_object *obj)
//将json_object转化为lh_table

3.struct array_list * json_object_get_array (struct json_object *obj)    //将

json_object转化为array_list

4,boolean json_object_get_boolean (struct json_object *obj)
//将json_object转化为boolean

5.int json_object_get_int (struct json_object *obj)
//将json_object转化为int

6,double json_object_get_double (struct json_object *obj)
//将json_object 转化为double

7.const char * json_object_get_string (struct json_object *obj)
//将json_object转化为char *

七,json_object.h:销毁一个json对象:

void json_object_put (struct json_object *obj)


八,json_object.h:判断json_object的类型是否为基础数据类型type,是:0,否:1:

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


九,json_object.h:获得json_object的基础类型:

enum json_type json_object_get_type (struct json_object *obj)


其中:enum json_type 为:

Enumerator:

json_type_null  
json_type_boolean  
json_type_double  
json_type_int  
json_type_object  
json_type_array  
json_type_string  


十,json_object.h:json数组:注意:json数组本身也为json_object,其为基类

1,创建json数组:

struct json_object * json_object_new_array (void)


2,将json数组转化为arraylist:

struct array_list * json_object_get_array (struct json_object *obj)


3,获得json数组的长度:

int json_object_array_length (struct json_object *obj)


4,向json数组obj内添加一个json_object:val

int json_object_array_add (struct json_object *obj, struct json_object *val)
,其中obj为json数组,val为需要添加的对象

5,Insert or replace an element at a specified index in an array (a json_object of type json_type_array)

int json_object_array_put_idx (struct json_object *obj, int idx, struct json_object *val)
//向obj中的idx位置,update或insert 对象val

6,获得json数组中的某一特定的对象:

struct json_object * json_object_array_get_idx (struct json_object *obj, int idx)


3.struct json_object * json_object_object_get (struct json_object *obj, const char *key)


//从json_object对象中,通过key,获得value,例如:

json_object *req_json = NULL;    json_object_object_get(req_json,"ver"));

转载地址:http://magyi.baihongyu.com/

你可能感兴趣的文章
JSP中EL表达式无效问题
查看>>
java中关于时间日期操作的常用函数
查看>>
Serializable java序列化
查看>>
用Eclipse MyEclipse WebLogic8.1开发第一个Web程序
查看>>
HTTP深入浅出
查看>>
http协议技术资料
查看>>
MyEclipse安装aptana插件的问题
查看>>
Android环境搭建_转载
查看>>
JS操作SELECT表单大全,赋默认值,取值,增,删等
查看>>
浅谈BigDecimal类在电子商务中至关重要的地位!
查看>>
输出的数字的格式DecimalFormat的方法用途
查看>>
如何使用spring的作用域:
查看>>
Tomcat DBCP 连接池参数说明
查看>>
hibernate集合映射inverse和cascade详解
查看>>
使用SSH框架的好处
查看>>
attachEvent、addEventListener、detachEvent、removeEventListener
查看>>
flex myeclipse安装.
查看>>
hibernate中get 与 load 区别
查看>>
JSP文件下载及getOutputStream() has already been的解决
查看>>
Tomcat 6.0 开发配置小结
查看>>