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);
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对象
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对象
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));
}
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对象