欢迎进入C/C++编程社区论坛,与300万技术人员互动交流 >>进入
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 json_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对象)
[1] [2] 下一页
【责编:peter】