json-c 库的使用方法

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/networkhunter/article/details/97398044

json-c 库的使用方法

引入json-c的库

  • 使用对应的工具量编译json-c库,编译为动态链接和静态链接都可以,根据需求来决定。
  • 在应用程序中引入json-c的库,下面就可以调用json-c的函数对数据进行操作。

json-c的读写操作

读json-c

json_object *pobj = NULL
pobj = json_object_from_file("./bob.json");
//读文件里的数据
pval = json_object_object_get(jobj, sname);
type = json_object_get_type(pval);
//在根据不同类型来读取
switch(type)
{
case json_type_string:
printf(“Key:%s value: %s\n”, sname, json_object_get_string(pval));
break;
case json_type_int:
printf(“Key:%s value: %d\n”, sname, json_object_get_int(pval));
break;
//释放json对象
json_object_put(pobj);

添加key值对

json_object *pobj = NULL;
pobj = json_object_from_file("/home/boy/tmp/test/jsontest/test.json"); //把文件转为json对象
json_object_object_add(pobj, “foo”, json_object_new_string(“fark”)); //json对象添加
json_object_to_file("/home/boy/tmp/test/jsontest/new.json", pobj); //把json对象写回文件
json_object_put(pobj); //释放jison对象

删除key只对

json_object *pobj = NULL;
pobj = json_object_from_file("/home/boy/tmp/test/jsontest/test.json"); //把文件转为json对象
json_object_object_del(pobj, “foo”); //json对象删除
json_object_to_file("/home/boy/tmp/test/jsontest/new.json", pobj); //把json对象写回文件
json_object_put(pobj); //释放jison对象

修改key值对

json_object *pobj = NULL;
pobj = json_object_from_file("/home/boy/tmp/test/jsontest/test.json"); //把文件转为json对象
json_object_object_del(pobj, “foo”); //json对象删除
json_object_object_add(pobj, “foo”, json_object_new_string(“fark”)); //json对象添加
json_object_to_file("/home/boy/tmp/test/jsontest/new.json", pobj); //把json对象写回文件
json_object_put(pobj); //释放jison对象

怎么添加数组

json_object *my_array = json_object_new_array();
json_object_array_add(my_array, json_object_new_int(1));
json_object_array_add(my_array, json_object_new_int(2));
json_object_array_add(my_array, json_object_new_int(3));
json_object_array_put_idx(my_array, 4, json_object_new_int(5));
for(i=0; i < json_object_array_length(my_array); i++) {
struct json_object *obj = json_object_array_get_idx(my_array, i);
printf("\t[%d]=%s\n", i, json_object_to_json_string(obj));
}

printf(“my_array.to_string()=%s\n”, json_object_to_json_string(my_array));
my_object = json_object_new_object();
/添加json名称和值到json对象集合中/
json_object_object_add(my_object, “abc”, json_object_new_int(12));
json_object_object_add(my_object, “foo”, json_object_new_string(“bar”));
json_object_object_add(my_object, “bool0”, json_object_new_boolean(0));
json_object_object_add(my_object, “bool1”, json_object_new_boolean(1));
json_object_object_add(my_object, “baz”, json_object_new_string(“bang”));
/同样的key 添加会替换掉/
json_object_object_add(my_object, “baz”, json_object_new_string(“fark”));
json_object_object_del(my_object, “baz”);
/添加数组集合到json对象中/
json_object_object_add(my_object, “arr”, my_array);
printf(“my_object=\n”);
/遍历json对象集合/
json_object_object_foreach(my_object, key, val) {
printf("\t%s: %s\n", key, json_object_to_json_string(val));
}

jason-c怎么读回数组中的元素?

json_object *pobj = NULL;
pobj = json_object_from_file("./bob.json");
key_obj = json_object_object_get(jobj, “key”);
for(i=0; i < json_object_array_length(key_obj); i++) {
struct json_object *obj = json_object_array_get_idx(key_obj, i);
printf("\t[%d]=%d\n", i, json_object_get_int(obj));
}
json_object_put(key_obj);
json_object_put(pobj); //释放jison对象

展开阅读全文

没有更多推荐了,返回首页