创建json对象
(1)函数:
extern struct json_object* json_object_new_object(void);
函数说明: 创建一个新的json对象,引用计数1,该具有唯一的所有权。当使用json_object_object_add或者json_object_array_put_idx作用于该对象时,所有权转移到另一方。使用json_object_get作用于该对象的所有权,必须使用json_object_put释放。
(2)函数:
extern struct json_object* json_object_new_array(void);
函数说明: 创建的json数组类型对象
(3)创建一个json 的bool对象。
extern struct json_object* json_object_new_boolean(json_bool b);
(4)创建josn的int 32、64对象
extern struct json_object* json_object_new_int(int32_t i);
extern struct json_object* json_object_new_int64(int64_t i);
(5)将c字符串转换为json字符串格式的对象,该对象堆上分配
extern struct json_object* json_object_new_string(const char *s);
extern struct json_object* json_object_new_string_len(const char *s, int len);
(6)创建double类型的json对象
extern struct json_object* json_object_new_double(double d);
增加、删除、修改
(1)给json对象增加字段
引用计数不会增加,val的所有权转移到obj。
extern void json_object_object_add(struct json_object* obj, const char *key,struct json_object *val);
(2)删除json对象的指定字段,被删除的对象引用计数减去1,如果这个val没有更多的所有者,这个key对应的val被free,否则这个val的引用保存在内存中
extern void json_object_object_del(struct json_object* obj, const char *key);
(3)增加一个元素到json数组的末尾。
obj引用计数不会增加,增加字段的方式更加紧凑。
如果需要获取val的引用,需要用json_object_get来传递该对象。
extern int json_object_array_add(struct json_object *obj, struct json_object *val);
(4)替换json数组中的值
extern int json_object_array_put_idx(struct json_object *obj, int idx, struct json_object *val);
(5)json数组的排序,这里需要自己写排序函数
extern void json_object_array_sort(struct json_object *jso, int(*sort_fn)(const void*, const void *));
取值
(1)获取json对象的长度,依据字段的数目
extern intjson_object_object_length(struct json_object* obj);
(2)获取json对象的哈希表
extern struct lh_table*json_object_get_object(struct json_object *obj);
(3)从obj中获取key对应的json对象,引用计数没有改变
extern struct json_object*json_object_object_get(struct json_object* obj,const char *key);
(4)获取对象的数组列表
extern struct array_list*json_object_get_array(struct json_object *obj);
(5)获取json的类型
extern enum json_type json_object_get_type(struct json_object *obj);
(6)获取json数组对象的长度
extern intjson_object_array_length(struct json_object *obj);
(7)获取json对象bool值。int和double对象是0转换为FALSE,否则返回TRUE。
非0长度的字符串返回TRUE。其他对象非空的话,返回TRUE。
extern json_booljson_object_get_boolean(struct json_object *obj);
(8)获取json对象的长度,如果参数不是string类型的json,返回0
extern intjson_object_get_string_len(struct json_object *obj);
(9)按照索引获取json数组的对象
extern struct json_object* json_object_array_get_idx(struct json_object *obj, intidx);
类型转换
(1)转换json对象到c字符串格式
extern const char*json_object_to_json_string(struct json_object *obj);
(2)如果参数非json类型的字符串,返回then the JSON representation of the object is returned.参数对应的json对象内存空间,当其引用计数为0时,该内存free。obj是json对象实例;回c格式的字符串;
extern const char* json_object_get_string(struct json_object *obj);
(3)同上,不同的是flags: formattingoptions, see JSON_C_TO_STRING_PRETTY and other constants
extern const char* json_object_to_json_string_ext(struct json_object *obj, int flags);
(4)如果传递的非int,强制转换为int。double对象返回其int。字符串解析为int。
如果没有转换存在返回0.并且error置为EINVVAL,0和null等价。
整形以64位值存储,如果太小或是太大,不足以填充32位,将返回INT32_MAX,INT32_MIN
extern int32_t json_object_get_int(struct json_object *obj);
(5)获取json对象的int64_t、double值
extern int64_t json_object_get_int64(struct json_object *obj);
extern double json_object_get_double(struct json_object *obj);
(6)将字符串转换为json对象
extern struct json_object* json_tokener_parse(const char *str);
json对象的释放
以下两个函数配合使用,前者获取该对象指针的所有权, 引用计数加1,如果对象已经被释放,返回1,后者引用计数减1,如果对象已经被释放,返回1
extern struct json_object* json_object_get(struct json_object *obj);
extern int json_object_put(struct json_object *obj);
其他
(1) 类型判断
externint json_object_is_type(struct json_object *obj, enum json_type type);