c语言json函数库,json库函数入门例子,详解。--C语言

// 还是得多动手总结啊,不足之处,还望提醒。编译的时候,记得加上 -ljson 库

#include

#include "json/json.h"

void make_a_common_json_string();

int main()

{

struct json_object *my_obj;

//json格式的字符串,\"string\".(特殊字符加转义符)

char *str="{\"abc\": 123, \"wds\": 12.3, \"qwe\": \"ddd\", \"bool0\": false, \"bool1\": true, \"arr\": [12, 3, 0, 5, null]}";

//将符合json格式的字符串构造为一个json对象

my_obj = json_tokener_parse(str);

printf("1-tokener parse: %s\n", json_object_to_json_string(my_obj));

// 在对象里用key值去找,然后删除它的key:value.

json_object_object_del(my_obj, "abc");

printf("2-tokener parse: %s\n", json_object_to_json_string(my_obj));

struct json_object *ret_obj;

// 在对象里用key值去找,获取它的value值

ret_obj = json_object_object_get(my_obj, "wds");

printf("obj_get-wds:%s\n", json_object_to_json_string(ret_obj));

// 在json_object_object_get 引用一次。

// 将jso对象的引用计数减1,子对象引用计数为0时释放所占用内存。

json_object_put(ret_obj);

// 获取不同的json数据类型,int.double.string.array.object.

struct json_object *val = json_object_new_int(18542);

// 将key:value加入到对象my_obj中.

json_object_object_add(my_obj, "wds", val);

printf("3-object add: %s\n", json_object_to_json_string(my_obj));

// 看function详解1

json_object_put(val);

printf("get string:%s\n", json_object_get_string(my_obj));

printf("get lh:%d\n", json_object_get_object(my_obj));

printf("get int:%d\n", json_object_get_int(my_obj));

make_a_common_json_string();

// 看function详解1

json_object_put(my_obj);

return 0;

}

// 这个接口,包含了很多数据类型(array, int, string, object)。组成json包,socket传输,为上层提供数据,便于展示。

void make_a_common_json_string()

{

printf("####################################\n");

int i=0;

struct json_object *obj_all = json_object_new_object();

struct json_object *obj_info = json_object_new_object();

struct json_object *obj_work = json_object_new_object();

struct json_object *obj_life = json_object_new_object();

struct json_object *arr_month_grade = json_object_new_array();

for(i=1; i<13; i++){

struct json_object *obj_int = json_object_new_int(i);

json_object_array_add(arr_month_grade, obj_int);

}

//const char *str = "{\"职位\": \"programmer\", \"部门\": \"研发\", \"addres\": \"BJ.3\"}";

//json_object *obj_str = json_tokener_parse(str);

json_object_object_add(obj_work, "position", json_object_new_string("programmer"));

json_object_object_add(obj_work, "department", json_object_new_string("make code"));

json_object_object_add(obj_work, "address", json_object_new_string("BJ.3.103"));

json_object_object_add(obj_work, "mounth_grade", arr_month_grade);

json_object_object_add(obj_info, "Work", obj_work);

json_object_object_add(obj_life, "Name", json_object_new_string("DS.wen"));

json_object_object_add(obj_life, "Age", json_object_new_int(24.34));

json_object_object_add(obj_life, "M-O", json_object_new_string("Male"));

json_object_object_add(obj_info, "Life", obj_life);

json_object_object_add(obj_all,"W.DS_Info", obj_info);

printf("all_string:%s\n", json_object_to_json_string(obj_all));

json_object_put(obj_all);

return ;

}

/*************************************打印结果********************************************/

1-tokener parse: { "abc": 123, "wds": 12.300000, "qwe": "ddd", "bool0": false, "bool1": true, "arr": [ 12, 3, 0, 5, null ] }

2-tokener parse: { "wds": 12.300000, "qwe": "ddd", "bool0": false, "bool1": true, "arr": [ 12, 3, 0, 5, null ] }

obj_get-wds:12.300000

3-object add: { "wds": 18542, "qwe": "ddd", "bool0": false, "bool1": true, "arr": [ 12, 3, 0, 5, null ] }

get string:{ "wds": 18542, "qwe": "ddd", "bool0": false, "bool1": true, "arr": [ 12, 3, 0, 5, null ] }

get lh:26383664

get int:0

####################################

all_string:{ "W.DS_Info": { "Work": { "position": "programmer", "department": "make code", "address": "BJ.3.103", "mounth_grade": [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] }, "Life": { "Name": "DS.wen", "Age": 24, "M-O": "Male" } } }

/********************************************************************************************************************/

/-----------------------------function--------------------------------------/

1. struct json_object * * json_object_get(struct json_object * this)

说明:

增加对象引用计数。使用c库最关心的是内存谁来分配, 谁来释放. jsonc的内存管理方式, 是基于引用计数的内存树(链), 如果把一个struct json_object 对象a, add到另一个对象b上, 就不用显式的释放(json_object_put) a了, 相当于把a挂到了b的对象树上, 释放b的时候, 就会释放a. 当a即add到b上, 又add到对象c上时会导致a被释放两次(double free), 这时可以增加a的引用计数(调用函数json_object_get(a)), 这时如果先释放b, 后释放c, 当释放b时, 并不会真正的释放a, 而是减少a的引用计数为1, 然后释放c时, 才真正释放a.

参数:

this – json对象

Void json_object_put(struct json_object * this)

说明:

减少对象引用次数一次,当减少到0就释放(free)资源

参数:

this – json对象

注:如果对json对象显式调用了json_object_get,之后必须成对调用json_object_put,否则将导致该json对象所占用内存泄漏。 2. json_object_new_XXX系列函数 struct json_object* json_object_new_object(void); struct json_object* json_object_new_int(int i); struct json_object* json_object_new_double(double d); struct json_object* json_object_new_array(void); struct json_object* json_object_new_string(const char *s); struct json_object* json_object_new_string_len(const char *s, int len); json_object_new_XXX系列函数用来创建XXX类型的json对象,创建的json对象默认引用计数为1,因此在该对象使用完后也需要调用一次json_object_put来把引用计数置0,从而释放内存。 3. json_tokener_parse函数 struct json_object* json_tokener_parse(const char *str); json_tokener_parse将符合json格式的字符串构造为一个json对象,构造的json对象默认引用计数为1,同样需要在使用完后对该对象调用一次json_object_put。 4. is_error宏 is_error(jso) 如果传入的字符串是非法的json格式,错误判断应该使用is_error宏,而非 if(NULL != jso),CGI中目前有很多这种错误用法(虽然好像没引发什么问题) 5. json_object_object_XXX函数 void json_object_object_del(struct json_object* jso, const char *key); 从jso对象中删除键值为key的子对象,并释放该子对象及键值所占的内存(注:可能有文档说json_object_object_del只是删除而不释放内存,但实际上这是错误的)。 struct json_object* json_object_object_get(struct json_object* jso, const char *key); 从jso中获取键值为key的子对象。错误判断同样应该用is_error(jso)宏。 void json_object_object_add(struct json_object* jso, const char *key, struct json_object *val); 更新键值为key的子项的值。整个过程实际上是这样的:先从jso中删除并释放键值key及其值的内存,然后重新分配内存添加键值和新的值,所以json_object_object_add是包含json_object_object_del操作的。 6. json_object_get_XXX系列函数 struct lh_table* json_object_get_object(struct json_object *jso); const char* json_object_get_string(struct json_object *jso); int json_object_get_int(struct json_object *jso); double json_object_get_double(struct json_object *jso); 这类函数只是获取jso对象的实际数据内容,不更新引用计数,也不分配内存。 7. json_object_array_XXX系列函数 struct json_object* json_object_array_get_sub_array(struct json_object *jso, int start_idx, int number); 这个函数用来从一个json数组对象中取数组序号start_idx开始的、总共number长度的子数组对象。分页显示功能常用到。注:返回的子数组是有重新分配内存的,所以同样要对返回的json_object*做一次json_object_put操作来释放内存。 int json_object_array_add(struct json_object *jso,struct json_object *val); 向数组中添加一个值。 int json_object_array_length(struct json_object *jso); 获取数组长度。 int json_object_array_put_idx(struct json_object *jso, int idx, struct json_object *val); 更新数组中序号为idx那一项的值,老的值同样会先被释放。 struct json_object* json_object_array_get_idx(struct json_object *jso, int idx); 获取数组中序号为idx那一项的json对象,不更新引用计数,也不分配内存。 struct json_object* json_object_array_sort(struct json_object *jso, const char *keyname, int sord ); 根据键值为keyname的项的值进行升序或降序排序,只是改变数组成员的顺序,不更新引用计数,也不分配内存。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值