Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3125198
  • 博文数量: 685
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 5303
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-19 14:17
个人简介

文章分类

全部博文(685)

文章存档

2015年(116)

2014年(569)

分类: 嵌入式

2014-11-13 15:47:48

原文地址:http://blog.csdn.net/a252228180/article/details/7367743


总结json-c-0.9 在linux上编译使用的方法,和一个简单的例子。


1.

将json-c-0.9解压,这里选择目录/home/lesterpang/fs/json-c-0.9


2.

指令下述命令


  1. # cd /home/lesterpang/fs/json-c-0.9/
  2. # ./configure
  3. # make
  4. # 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目录


  1. include /usr/local/lib


执行完上述步骤并未出现错误后,编译安装完成,下面给一使用例子。


5.

此代码来自《JSON c 语言开发指南》,作者不详。

代码如下:


  1. 1 #include
  2. 2 #include
  3. 3 #include
  4. 4 #include
  5. 5
  6. 6 #include "json.h"
  7. 7
  8. 8 int main(int argc, char **argv)
  9. 9 {
  10. 10 struct json_tokener *tok;
  11. 11 struct json_object *my_string, *my_int, *my_object, *my_array;
  12. 12 struct json_object *new_obj;
  13. 13 int i;
  14. 14
  15. 15 my_string = json_object_new_string("\t");
  16. 16
  17. 17 printf("my_string=%s\n", json_object_get_string(my_string));
  18. 18 printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
  19. 19 json_object_put(my_string);
  20. 20
  21. 21 my_string = json_object_new_string("\\");
  22. 22 printf("my_string=%s\n", json_object_get_string(my_string));
  23. 23 printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
  24. 24 json_object_put(my_string);
  25. 25
  26. 26 my_string = json_object_new_string("foo");
  27. 27 printf("my_string=%s\n", json_object_get_string(my_string));
  28. 28 printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
  29. 29
  30. 30 my_int = json_object_new_int(9);
  31. 31 printf("my_int=%d\n", json_object_get_int(my_int));
  32. 32 printf("my_int.to_string()=%s\n", json_object_to_json_string(my_int));
  33. 33
  34. 34 my_array = json_object_new_array();
  35. 35 json_object_array_add(my_array, json_object_new_int(1));
  36. 36 json_object_array_add(my_array, json_object_new_int(2));
  37. 37 json_object_array_add(my_array, json_object_new_int(3));
  38. 38 json_object_array_put_idx(my_array, 4, json_object_new_int(5));
  39. 39 printf("my_array=\n");
  40. 40 for(i=0; i < json_object_array_length(my_array); i++) {
  41. 41 struct json_object *obj = json_object_array_get_idx(my_array, i);
  42. 42 printf("\t[%d]=%s\n", i, json_object_to_json_string(obj));
  43. 43 }
  44. 44 printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array));
  45. 45
  46. 46 my_object = json_object_new_object();
  47. 47 json_object_object_add(my_object, "abc", json_object_new_int(12));
  48. 48 json_object_object_add(my_object, "foo", json_object_new_string("bar"));
  49. 49 json_object_object_add(my_object, "bool0", json_object_new_boolean(0));
  50. 50 json_object_object_add(my_object, "bool1", json_object_new_boolean(1));
  51. 51 json_object_object_add(my_object, "baz", json_object_new_string("bang"));
  52. 52 json_object_object_add(my_object, "baz", json_object_new_string("fark"));
  53. 53 json_object_object_del(my_object, "baz");
  54. 54
  55. 55 printf("my_object=\n");
  56. 56 json_object_object_foreach(my_object, key, val) {
  57. 57 printf("\t%s: %s\n", key, json_object_to_json_string(val));
  58. 58 }
  59. 59 printf("my_object.to_string()=%s\n", json_object_to_json_string(my_object));
  60. 60
  61. 61 new_obj = json_tokener_parse("\"\003\"");
  62. 62 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  63. 63 json_object_put(new_obj);
  64. 64
  65. 65 new_obj = json_tokener_parse("/* hello */\"foo\"");
  66. 66 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  67. 67 json_object_put(new_obj);
  68. 68
  69. 69 new_obj = json_tokener_parse("// hello\n\"foo\"");
  70. 70 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  71. 71 json_object_put(new_obj);
  72. 72
  73. 73 new_obj = json_tokener_parse("\"\\u0041\\u0042\\u0043\"");
  74. 74 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  75. 75 json_object_put(new_obj);
  76. 76
  77. 77 new_obj = json_tokener_parse("null");
  78. 78 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  79. 79 json_object_put(new_obj);
  80. 80
  81. 81 new_obj = json_tokener_parse("True");
  82. 82 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  83. 83 json_object_put(new_obj);
  84. 84
  85. 85 new_obj = json_tokener_parse("12");
  86. 86 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  87. 87 json_object_put(new_obj);
  88. 88
  89. 89
  90. 90 new_obj = json_tokener_parse("12.3");
  91. 91
  92. 92 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  93. 93 json_object_put(new_obj);
  94. 94
  95. 95 new_obj = json_tokener_parse("[\"\\n\"]");
  96. 96 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  97. 97 json_object_put(new_obj);
  98. 98
  99. 99 new_obj = json_tokener_parse("[\"\\nabc\\n\"]");
  100. 100 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  101. 101 json_object_put(new_obj);
  102. 102
  103. 103 new_obj = json_tokener_parse("[null]");
  104. 104 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  105. 105 json_object_put(new_obj);
  106. 106
  107. 107 new_obj = json_tokener_parse("[]");
  108. 108 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  109. 109 json_object_put(new_obj);
  110. 110
  111. 111 new_obj = json_tokener_parse("[false]");
  112. 112 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  113. 113 json_object_put(new_obj);
  114. 114
  115. 115 new_obj = json_tokener_parse("[\"abc\",null,\"def\",12]");
  116. 116 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  117. 117 json_object_put(new_obj);
  118. 118
  119. 119 new_obj = json_tokener_parse("{}");
  120. 120 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  121. 121 json_object_put(new_obj);
  122. 122
  123. 123 new_obj = json_tokener_parse("{ \"foo\": \"bar\" }");
  124. 124 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  125. 125 json_object_put(new_obj);
  126. 126
  127. 127 new_obj = json_tokener_parse("{ \"foo\": \"bar\", \"baz\": null, \"bool0\": true }");
  128. 128 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  129. 129 json_object_put(new_obj);
  130. 130
  131. 131 new_obj = json_tokener_parse("{ \"foo\": [null, \"foo\"] }");
  132. 132 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  133. 133 json_object_put(new_obj);
  134. 134
  135. 135 new_obj = json_tokener_parse("{ \"abc\": 12, \"foo\": \"bar\", \"bool0\": false, \"bool1\": true, \"arr\": [ 1, 2, 3, null, 5 ] }");
  136. 136 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  137. 137 json_object_put(new_obj);
  138. 138
  139. 139 new_obj = json_tokener_parse("{ foo }");
  140. 140 if(is_error(new_obj)) printf("got error as expected\n");
  141. 141
  142. 142 new_obj = json_tokener_parse("foo");
  143. 143 if(is_error(new_obj)) printf("got error as expected\n");
  144. 144
  145. 145 new_obj = json_tokener_parse("{ \"foo");
  146. 146 if(is_error(new_obj)) printf("got error as expected\n");
  147. 147
  148. 148 tok = json_tokener_new();
  149. 149 new_obj = json_tokener_parse_ex(tok, "{ \"foo", 6);
  150. 150 if(is_error(new_obj)) printf("got error as expected\n");
  151. 151 new_obj = json_tokener_parse_ex(tok, "\": {\"bar", 8);
  152. 152 if(is_error(new_obj)) printf("got error as expected\n");
  153. 153 new_obj = json_tokener_parse_ex(tok, "\":13}}", 6);
  154. 154 printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  155. 155 json_object_put(new_obj);
  156. 156 json_tokener_free(tok);
  157. 157
  158. 158 json_object_put(my_string);
  159. 159 json_object_put(my_int);
  160. 160 json_object_put(my_object);
  161. 161 json_object_put(my_array);
  162. 162
  163. 163 return 0;
  164. 164 }


Makefile文件内容如下:


  1. 1 json_test:json_test.c
  2. 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

其中 -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 } }

阅读(2262) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~