本文共 4155 字,大约阅读时间需要 13 分钟。
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_get1,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_table3.struct array_list * json_object_get_array (struct json_object *obj) //将
json_object转化为array_list4,boolean json_object_get_boolean (struct json_object *obj)
//将json_object转化为boolean5.int json_object_get_int (struct json_object *obj)
//将json_object转化为int6,double json_object_get_double (struct json_object *obj)
//将json_object 转化为double7.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:valint 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 对象val6,获得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://xlkub.baihongyu.com/