总结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
# 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
-
2 #include
-
3 #include
-
4 #include
-
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=%s\n", json_object_get_string(my_string));
-
18 printf("my_string.to_string()=%s\n", 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=%s\n", json_object_get_string(my_string));
-
23 printf("my_string.to_string()=%s\n", 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=%s\n", json_object_get_string(my_string));
-
28 printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
-
29
-
30 my_int = json_object_new_int(9);
-
31 printf("my_int=%d\n", json_object_get_int(my_int));
-
32 printf("my_int.to_string()=%s\n", 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]=%s\n", i, json_object_to_json_string(obj));
-
43 }
-
44 printf("my_array.to_string()=%s\n", 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: %s\n", key, json_object_to_json_string(val));
-
58 }
-
59 printf("my_object.to_string()=%s\n", json_object_to_json_string(my_object));
-
60
-
61 new_obj = json_tokener_parse("\"\003\"");
-
62 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
-
63 json_object_put(new_obj);
-
64
-
65 new_obj = json_tokener_parse("/* hello */\"foo\"");
-
66 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
-
67 json_object_put(new_obj);
-
68
-
69 new_obj = json_tokener_parse("// hello\n\"foo\"");
-
70 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
-
71 json_object_put(new_obj);
-
72
-
73 new_obj = json_tokener_parse("\"\\u0041\\u0042\\u0043\"");
-
74 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
-
75 json_object_put(new_obj);
-
76
-
77 new_obj = json_tokener_parse("null");
-
78 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
-
79 json_object_put(new_obj);
-
80
-
81 new_obj = json_tokener_parse("True");
-
82 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
-
83 json_object_put(new_obj);
-
84
-
85 new_obj = json_tokener_parse("12");
-
86 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
-
87 json_object_put(new_obj);
-
88
-
89
-
90 new_obj = json_tokener_parse("12.3");
-
91
-
92 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
-
93 json_object_put(new_obj);
-
94
-
95 new_obj = json_tokener_parse("[\"\\n\"]");
-
96 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
-
97 json_object_put(new_obj);
-
98
-
99 new_obj = json_tokener_parse("[\"\\nabc\\n\"]");
-
100 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
-
101 json_object_put(new_obj);
-
102
-
103 new_obj = json_tokener_parse("[null]");
-
104 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
-
105 json_object_put(new_obj);
-
106
-
107 new_obj = json_tokener_parse("[]");
-
108 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
-
109 json_object_put(new_obj);
-
110
-
111 new_obj = json_tokener_parse("[false]");
-
112 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
-
113 json_object_put(new_obj);
-
114
-
115 new_obj = json_tokener_parse("[\"abc\",null,\"def\",12]");
-
116 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
-
117 json_object_put(new_obj);
-
118
-
119 new_obj = json_tokener_parse("{}");
-
120 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
-
121 json_object_put(new_obj);
-
122
-
123 new_obj = json_tokener_parse("{ \"foo\": \"bar\" }");
-
124 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
-
125 json_object_put(new_obj);
-
126
-
127 new_obj = json_tokener_parse("{ \"foo\": \"bar\", \"baz\": null, \"bool0\": true }");
-
128 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
-
129 json_object_put(new_obj);
-
130
-
131 new_obj = json_tokener_parse("{ \"foo\": [null, \"foo\"] }");
-
132 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
-
133 json_object_put(new_obj);
-
134
-
135 new_obj = json_tokener_parse("{ \"abc\": 12, \"foo\": \"bar\", \"bool0\": false, \"bool1\": true, \"arr\": [ 1, 2, 3, null, 5 ] }");
-
136 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
-
137 json_object_put(new_obj);
-
138
-
139 new_obj = json_tokener_parse("{ foo }");
-
140 if(is_error(new_obj)) printf("got error as expected\n");
-
141
-
142 new_obj = json_tokener_parse("foo");
-
143 if(is_error(new_obj)) printf("got error as expected\n");
-
144
-
145 new_obj = json_tokener_parse("{ \"foo");
-
146 if(is_error(new_obj)) printf("got error as expected\n");
-
147
-
148 tok = json_tokener_new();
-
149 new_obj = json_tokener_parse_ex(tok, "{ \"foo", 6);
-
150 if(is_error(new_obj)) printf("got error as expected\n");
-
151 new_obj = json_tokener_parse_ex(tok, "\": {\"bar", 8);
-
152 if(is_error(new_obj)) printf("got error as expected\n");
-
153 new_obj = json_tokener_parse_ex(tok, "\":13}}", 6);
-
154 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
-
155 json_object_put(new_obj);
-
156 json_tokener_free(tok);
-
157
-
158 json_object_put(my_string);
-
159 json_object_put(my_int);
-
160 json_object_put(my_object);
-
161 json_object_put(my_array);
-
162
-
163 return 0;
-
164 }
1 #include
2 #include
3 #include
4 #include
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=%s\n", json_object_get_string(my_string));
18 printf("my_string.to_string()=%s\n", 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=%s\n", json_object_get_string(my_string));
23 printf("my_string.to_string()=%s\n", 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=%s\n", json_object_get_string(my_string));
28 printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
29
30 my_int = json_object_new_int(9);
31 printf("my_int=%d\n", json_object_get_int(my_int));
32 printf("my_int.to_string()=%s\n", 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]=%s\n", i, json_object_to_json_string(obj));
43 }
44 printf("my_array.to_string()=%s\n", 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: %s\n", key, json_object_to_json_string(val));
58 }
59 printf("my_object.to_string()=%s\n", json_object_to_json_string(my_object));
60
61 new_obj = json_tokener_parse("\"\003\"");
62 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
63 json_object_put(new_obj);
64
65 new_obj = json_tokener_parse("/* hello */\"foo\"");
66 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
67 json_object_put(new_obj);
68
69 new_obj = json_tokener_parse("// hello\n\"foo\"");
70 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
71 json_object_put(new_obj);
72
73 new_obj = json_tokener_parse("\"\\u0041\\u0042\\u0043\"");
74 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
75 json_object_put(new_obj);
76
77 new_obj = json_tokener_parse("null");
78 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
79 json_object_put(new_obj);
80
81 new_obj = json_tokener_parse("True");
82 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
83 json_object_put(new_obj);
84
85 new_obj = json_tokener_parse("12");
86 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
87 json_object_put(new_obj);
88
89
90 new_obj = json_tokener_parse("12.3");
91
92 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
93 json_object_put(new_obj);
94
95 new_obj = json_tokener_parse("[\"\\n\"]");
96 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
97 json_object_put(new_obj);
98
99 new_obj = json_tokener_parse("[\"\\nabc\\n\"]");
100 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
101 json_object_put(new_obj);
102
103 new_obj = json_tokener_parse("[null]");
104 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
105 json_object_put(new_obj);
106
107 new_obj = json_tokener_parse("[]");
108 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
109 json_object_put(new_obj);
110
111 new_obj = json_tokener_parse("[false]");
112 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
113 json_object_put(new_obj);
114
115 new_obj = json_tokener_parse("[\"abc\",null,\"def\",12]");
116 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
117 json_object_put(new_obj);
118
119 new_obj = json_tokener_parse("{}");
120 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
121 json_object_put(new_obj);
122
123 new_obj = json_tokener_parse("{ \"foo\": \"bar\" }");
124 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
125 json_object_put(new_obj);
126
127 new_obj = json_tokener_parse("{ \"foo\": \"bar\", \"baz\": null, \"bool0\": true }");
128 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
129 json_object_put(new_obj);
130
131 new_obj = json_tokener_parse("{ \"foo\": [null, \"foo\"] }");
132 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
133 json_object_put(new_obj);
134
135 new_obj = json_tokener_parse("{ \"abc\": 12, \"foo\": \"bar\", \"bool0\": false, \"bool1\": true, \"arr\": [ 1, 2, 3, null, 5 ] }");
136 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
137 json_object_put(new_obj);
138
139 new_obj = json_tokener_parse("{ foo }");
140 if(is_error(new_obj)) printf("got error as expected\n");
141
142 new_obj = json_tokener_parse("foo");
143 if(is_error(new_obj)) printf("got error as expected\n");
144
145 new_obj = json_tokener_parse("{ \"foo");
146 if(is_error(new_obj)) printf("got error as expected\n");
147
148 tok = json_tokener_new();
149 new_obj = json_tokener_parse_ex(tok, "{ \"foo", 6);
150 if(is_error(new_obj)) printf("got error as expected\n");
151 new_obj = json_tokener_parse_ex(tok, "\": {\"bar", 8);
152 if(is_error(new_obj)) printf("got error as expected\n");
153 new_obj = json_tokener_parse_ex(tok, "\":13}}", 6);
154 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
155 json_object_put(new_obj);
156 json_tokener_free(tok);
157
158 json_object_put(my_string);
159 json_object_put(my_int);
160 json_object_put(my_object);
161 json_object_put(my_array);
162
163 return 0;
164 }
Makefile文件内容如下:
-
1 json_test:json_test.c
-
2 "WHITE-SPACE: pre"> gcc -g -D__STRICT_ANSI__ -ljson -I/usr/local/include/json/ -L/usr/local/lib/ json_test.c -o json_test
1 json_test:json_test.c
2 gcc -g -D__STRICT_ANSI__ -ljson -I/usr/local/include/json/ -L/usr/local/lib/ json_test.c -o json_test
其中 -D__STRICT_ANSI__ 是为了消除c99的编译选项。
编译后执行结果如下:
my_string=
my_string.to_string()="\t"
my_string=\
my_string.to_string()="\\"
my_string=foo
my_string.to_string()="foo"
my_int=9
my_int.to_string()=9
my_array=
[0]=1
[1]=2
[2]=3
[3]=null
[4]=5
my_array.to_string()=[ 1, 2, 3, null, 5 ]
my_object=
abc: 12
foo: "bar"
bool0: false
bool1: true
my_object.to_string()={ "abc": 12, "foo": "bar", "bool0": false, "bool1": true }
new_obj.to_string()="\u0003"
new_obj.to_string()="foo"
new_obj.to_string()="foo"
new_obj.to_string()="ABC"
new_obj.to_string()=null
new_obj.to_string()=true
new_obj.to_string()=12
new_obj.to_string()=12.300000
new_obj.to_string()=[ "\n" ]
new_obj.to_string()=[ "\nabc\n" ]
new_obj.to_string()=[ null ]
new_obj.to_string()=[ ]
new_obj.to_string()=[ false ]
new_obj.to_string()=[ "abc", null, "def", 12 ]
new_obj.to_string()={ }
new_obj.to_string()={ "foo": "bar" }
new_obj.to_string()={ "foo": "bar", "baz": null, "bool0": true }
new_obj.to_string()={ "foo": [ null, "foo" ] }
new_obj.to_string()={ "abc": 12, "foo": "bar", "bool0": false, "bool1": true, "arr": [ 1, 2, 3, null, 5 ] }
got error as expected
got error as expected
got error as expected
new_obj.to_string()={ "foo": { "bar": 13 } }