概述
总结json-c-0.9 在linux上编译使用的方法,和一个简单的例子。
1.
将json-c-0.9解压,这里选择目录/home/lesterpang/fs/json-c-0.9
2.
指令下述命令
- # cd /home/lesterpang/fs/json-c-0.9/
- # ./configure
- # make
- # make install
我这里执行命令使用的root用户,在使用个人用户执行时,在make install处出现错误。
3.
查看install是否正确完成
在目录/usr/local/include/json/下有如下文件:
json.h
bits.h
debug.h
linkhash.h
arraylist.h
printbuf.h
json_util.h
json_object.h
json_object_private.h
json_tokener.h
在目录/usr/local/lib/下有如下文件:
libjson.so.0.0.1
libjson.so.0 -> libjson.so.0.0.1
libjson.so -> libjson.so.0.0.1
libjson.la
libjson.a
4.
在/etc/ld.so.conf文件中添加此lib目录
- include /usr/local/lib
执行完上述步骤并未出现错误后,编译安装完成,下面给一使用例子。
5.
此代码来自《JSON c 语言开发指南》,作者不详。
代码如下:
- 1 #include <stdio.h>
- 2 #include <stdlib.h>
- 3 #include <stddef.h>
- 4 #include <string.h>
- 5
- 6 #include "json.h"
- 7
- 8 int main(int argc, char **argv)
- 9 {
- 10 struct json_tokener *tok;
- 11 struct json_object *my_string, *my_int, *my_object, *my_array;
- 12 struct json_object *new_obj;
- 13 int i;
- 14
- 15 my_string = json_object_new_string("t");
- 16
- 17 printf("my_string=%sn", json_object_get_string(my_string));
- 18 printf("my_string.to_string()=%sn", json_object_to_json_string(my_string));
- 19 json_object_put(my_string);
- 20
- 21 my_string = json_object_new_string("\");
- 22 printf("my_string=%sn", json_object_get_string(my_string));
- 23 printf("my_string.to_string()=%sn", json_object_to_json_string(my_string));
- 24 json_object_put(my_string);
- 25
- 26 my_string = json_object_new_string("foo");
- 27 printf("my_string=%sn", json_object_get_string(my_string));
- 28 printf("my_string.to_string()=%sn", json_object_to_json_string(my_string));
- 29
- 30 my_int = json_object_new_int(9);
- 31 printf("my_int=%dn", json_object_get_int(my_int));
- 32 printf("my_int.to_string()=%sn", json_object_to_json_string(my_int));
- 33
- 34 my_array = json_object_new_array();
- 35 json_object_array_add(my_array, json_object_new_int(1));
- 36 json_object_array_add(my_array, json_object_new_int(2));
- 37 json_object_array_add(my_array, json_object_new_int(3));
- 38 json_object_array_put_idx(my_array, 4, json_object_new_int(5));
- 39 printf("my_array=n");
- 40 for(i=0; i < json_object_array_length(my_array); i++) {
- 41 struct json_object *obj = json_object_array_get_idx(my_array, i);
- 42 printf("t[%d]=%sn", i, json_object_to_json_string(obj));
- 43 }
- 44 printf("my_array.to_string()=%sn", json_object_to_json_string(my_array));
- 45
- 46 my_object = json_object_new_object();
- 47 json_object_object_add(my_object, "abc", json_object_new_int(12));
- 48 json_object_object_add(my_object, "foo", json_object_new_string("bar"));
- 49 json_object_object_add(my_object, "bool0", json_object_new_boolean(0));
- 50 json_object_object_add(my_object, "bool1", json_object_new_boolean(1));
- 51 json_object_object_add(my_object, "baz", json_object_new_string("bang"));
- 52 json_object_object_add(my_object, "baz", json_object_new_string("fark"));
- 53 json_object_object_del(my_object, "baz");
- 54
- 55 printf("my_object=n");
- 56 json_object_object_foreach(my_object, key, val) {
- 57 printf("t%s: %sn", key, json_object_to_json_string(val));
- 58 }
- 59 printf("my_object.to_string()=%sn", json_object_to_json_string(my_object));
- 60
- 61 new_obj = json_tokener_parse(""