XStream对JSON的支持
xStream对JSON也有非常好的支持,它提供了2个模型驱动。用这2个驱动可以完成Java对象到JSON的相互转换。使用JettisonMappedXmlDriver驱动,将Java对象转换成json,需要添加jettison.jar
一、准备工作
1、 下载jar包、及官方资源
xStream的jar下载地址:
https://nexus.codehaus.org/content/repositories/releases/com/thoughtworks/xstream/xstream-distribution/1.3.1/xstream-distribution-1.3.1-bin.zip
官方的示例很全,官方参考示例:http://xstream.codehaus.org/tutorial.html
添加xstream-1.3.1.jar文件到工程中,就可以开始下面的工作
1、 用JettisonMappedXmlDriver完成Java对象到JSON的转换
Java代码
1. /**
2. * <b>function:</b>XStream结合JettisonMappedXmlDriver驱动,转换Java对象到JSON
3. * 需要添加jettison jar
4. * @author hoojo
5. * @createDate Nov 27, 2010 1:23:18 PM
6. */
7. @Test
8. public void
9. "=======JettisonMappedXmlDriver===JavaObject >>>> JaonString=========");
10. new XStream(new
11. xstream.setMode(XStream.NO_REFERENCES);
12. "student", Student.class);
13. fail(xstream.toXML(bean));
14. }
运行后结果如下:
=======JettisonMappedXmlDriver===JavaObject >>>> JaonString=========
Jaon代码
1. {"student":{"id":1,"name":"jack","email":"jack@email.com","address":"china","birthday":[{},"2010-11-22"]}}
JSON的转换和XML的转换用法一样,只是创建XStream需要传递一个参数,这个参数就是xml到JSON映射转换的驱动。这里会降到两个驱动,分别是JettisonMappedXmlDriver、JsonHierarchicalStreamDriver。
2、 JsonHierarchicalStreamDriver完成Java对象到JSON的转换
Java代码
1. /**
2. * <b>function:</b>用XStream结合JsonHierarchicalStreamDriver驱动
3. * 转换java对象为JSON字符串
4. * @author hoojo
5. * @createDate Nov 27, 2010 1:16:46 PM
6. */
7. @Test
8. public void
9. "======JsonHierarchicalStreamDriver====JavaObject >>>> JaonString=========");
10. new XStream(new
11. //xstream.setMode(XStream.NO_REFERENCES);
12. "student", Student.class);
13. "-------Object >>>> JSON---------");
14. fail(xstream.toXML(bean));
15.
16. //failRed("========JsonHierarchicalStreamDriver==删除根节点=========");
17. //删除根节点
18. new XStream(new
19. public
20. return new
21. }
22. });
23. //xstream.setMode(XStream.NO_REFERENCES);
24. "student", Student.class);
25. fail(xstream.toXML(bean));
26. }
运行后结果如下:
======JsonHierarchicalStreamDriver====JavaObject >>>> JaonString=========
-------Object >>>> JSON---------
Json代码
1. {"student": {
2. "id": 1,
3. "name": "jack",
4. "email": "jack@email.com",
5. "address": "china",
6. "birthday": {
7. "birthday": "2010-11-22"
8. }
9. }}
10. {
11. "id": 1,
12. "name": "jack",
13. "email": "jack@email.com",
14. "address": "china",
15. "birthday": {
16. "birthday": "2010-11-22"
17. }
18. }
使用JsonHierarchicalStreamDriver转换默认会给转换后的对象添加一个根节点,但是在构建JsonHierarchicalStreamDriver驱动的时候,你可以重写createWriter方法,删掉根节点。
看上面的结果,一个是默认带根节点的JSON对象,它只是将类名作为一个属性,将对象作为该属性的一个值。而另一个没有带根属性的JSON就是通过重写createWriter方法完成的。
3、 将List集合转换成JSON字符串
Java代码
1. @Test
2. public void
3. "======JsonHierarchicalStreamDriver====JavaObject >>>> JaonString=========");
4. new
5. new
6. //xstream = new XStream(new JettisonMappedXmlDriver());//转换错误
7. //xstream.setMode(XStream.NO_REFERENCES);
8. "student", Student.class);
9.
10. new
11. //add
12.
13. new
14. "china");
15. "tom@125.com");
16. 2);
17. "tom");
18. new Birthday("2010-11-22");
19. bean.setBirthday(day);
20. //add
21.
22. new
23. "jack");
24. //add
25.
26. fail(xstream.toXML(list));
27.
28. //failRed("========JsonHierarchicalStreamDriver==删除根节点=========");
29. //删除根节点
30. new XStream(new
31. public
32. return new
33. }
34. });
35. "student", Student.class);
36. fail(xstream.toXML(list));
37. }
运行后结果如下
======JsonHierarchicalStreamDriver====JavaObject >>>> JaonString=========
Json代码
1. ##{"list": [
2. {
3. "id": 1,
4. "name": "jack",
5. "email": "jack@email.com",
6. "address": "china",
7. "birthday": {
8. "birthday": "2010-11-22"
9. }
10. },
11. {
12. "id": 2,
13. "name": "tom",
14. "email": "tom@125.com",
15. "address": "china",
16. "birthday": {
17. "birthday": "2010-11-22"
18. }
19. },
20. {
21. "id": 0,
22. "name": "jack"
23. }
24. ]}
25. #[
26. {
27. "id": 1,
28. "name": "jack",
29. "email": "jack@email.com",
30. "address": "china",
31. "birthday": {
32. "birthday": "2010-11-22"
33. }
34. },
35. {
36. "id": 2,
37. "name": "tom",
38. "email": "tom@125.com",
39. "address": "china",
40. "birthday": {
41. "birthday": "2010-11-22"
42. }
43. },
44. {
45. "id": 0,
46. "name": "jack"
47. }
48. ]
上面的list1是使用JsonHierarchicalStreamDriver 转换的,当然你也可以使用JettisonMappedXmlDriver驱动进行转换;用JettisonMappedXmlDriver转换后,你会发现格式不同而且没有根属性。
4、 Map转换json
Java代码
1. @Test
2. public void
3. "======JsonHierarchicalStreamDriver==== Map >>>> JaonString=========");
4. new XStream(new
5. //xstream = new XStream(new JettisonMappedXmlDriver());
6. "student", Student.class);
7.
8. new
9. "No.1", bean);//put
10.
11. new
12. "china");
13. "tom@125.com");
14. 2);
15. "tom");
16. new Birthday("2010-11-21"));
17. "No.2", bean);//put
18.
19. new
20. "jack");
21. "No.3", bean);//put
22.
23. fail(xstream.toXML(map));
24.
25. //failRed("========JsonHierarchicalStreamDriver==删除根节点=========");
26. //删除根节点
27. new XStream(new
28. public
29. return new
30. }
31. });
32. "student", Student.class);
33. fail(xstream.toXML(map));
34. }
运行后结果如下:
======JsonHierarchicalStreamDriver==== Map >>>> JaonString=========
Json代码
1. {"map": [
2. [
3. "No.3",
4. {
5. "id": 0,
6. "name": "jack"
7. }
8. ],
9. [
10. "No.1",
11. {
12. "id": 1,
13. "name": "jack",
14. "email": "jack@email.com",
15. "address": "china",
16. "birthday": {
17. "birthday": "2010-11-22"
18. }
19. }
20. ],
21. [
22. "No.2",
23. {
24. "id": 2,
25. "name": "tom",
26. "email": "tom@125.com",
27. "address": "china",
28. "birthday": {
29. "birthday": "2010-11-21"
30. }
31. }
32. ]
33. ]}
34. [
35. [
36. "No.3",
37. {
38. "id": 0,
39. "name": "jack"
40. }
41. ],
42. [
43. "No.1",
44. {
45. "id": 1,
46. "name": "jack",
47. "email": "jack@email.com",
48. "address": "china",
49. "birthday": {
50. "birthday": "2010-11-22"
51. }
52. }
53. ],
54. [
55. "No.2",
56. {
57. "id": 2,
58. "name": "tom",
59. "email": "tom@125.com",
60. "address": "china",
61. "birthday": {
62. "birthday": "2010-11-21"
63. }
64. }
65. ]
66. ]
5、 将JSON转换java对象
Java代码
1. /**
2. * <b>function:</b>JsonHierarchicalStreamDriver可以将简单的json字符串转换成java对象,list、map转换不成功;
3. * JsonHierarchicalStreamDriver读取JSON字符串到java对象出错
4. * @author hoojo
5. * @createDate Nov 27, 2010 1:22:26 PM
6. * @throws JSONException
7. */
8. @Test
9. public void readJSON2Object() throws
10. "{\"student\": {"
11. "\"id\": 1,"
12. "\"name\": \"haha\","
13. "\"email\": \"email\","
14. "\"address\": \"address\","
15. "\"birthday\": {"
16. "\"birthday\": \"2010-11-22\""
17. "}"
18. "}}";
19. //JsonHierarchicalStreamDriver读取JSON字符串到java对象出错,但JettisonMappedXmlDriver可以
20. new XStream(new
21. "student", Student.class);
22. fail(xstream.fromXML(json).toString());
23.
24. //JettisonMappedXmlDriver转换List集合出错,但JsonHierarchicalStreamDriver可以转换正确
25. //JettisonMappedXmlDriver 转换的字符串 {"list":{"student":[{"id":1,"name":"haha","email":"email","address":"address","birthday":[{},"2010-11-22"]}]},"student":{"id":2,"name":"tom","email":"tom@125.com","address":"china","birthday":[{},"2010-11-22"]}}
26. "{\"list\": [{"
27. "\"id\": 1,"
28. "\"name\": \"haha\","
29. "\"email\": \"email\","
30. "\"address\": \"address\","
31. "\"birthday\": {"
32. "\"birthday\": \"2010-11-22\""
33. "}"
34. "},{"
35. "\"id\": 2,"
36. "\"name\": \"tom\","
37. "\"email\": \"tom@125.com\","
38. "\"address\": \"china\","
39. "\"birthday\": {"
40. "\"birthday\": \"2010-11-22\""
41. "}"
42. "}]}";
43. //用js转换成功
44. List list = (List) xstream.fromXML(json);
45. //0好像转换失败
46. }
运行后结果如下:
Json代码
1. haha#1#address#2010-11-22#email
2. {"list": [{"id": 1,"name": "haha","email": "email","address": "address","birthday": {"birthday": "2010-11-22"}},
3. {"id": 2,"name": "tom","email": "tom@125.com","address": "china","birthday": {"birthday": "2010-11-22"}}]}
4. 0
JSON到Java的转换是fromXML方法