json-c API总结

最新版本: https://github.com/json-c/json-c

编译假如出错时:忽略指定库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.structjson_object*json_tokener_parse(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"));

  • 20
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 17
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

keivin2006

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值