xStream完美转换XML、JSON(2)
2、 JsonHierarchicalStreamDriver完成Java对象到JSON的转换
/**
- <b>function:</b>用XStream结合JsonHierarchicalStreamDriver驱动
- 转换java对象为JSON字符串
- @author hoojo
-
@createDate Nov 27, 2010 1:16:46 PM
*/
@Test
public void writeEntiry2JSON() {
failRed("======JsonHierarchicalStreamDriver====JavaObject >>>> JaonString=========");
xstream = new XStream(new JsonHierarchicalStreamDriver());
//xstream.setMode(XStream.NO_REFERENCES);
xstream.alias("student", Student.class);
failRed("-------Object >>>> JSON---------");
fail(xstream.toXML(bean));//failRed("========JsonHierarchicalStreamDriver==删除根节点=========");
//删除根节点
xstream = new XStream(new JsonHierarchicalStreamDriver() {
public HierarchicalStreamWriter createWriter(Writer out) {
return new JsonWriter(out, JsonWriter.DROP_ROOT_MODE);
}
});
//xstream.setMode(XStream.NO_REFERENCES);
xstream.alias("student", Student.class);
fail(xstream.toXML(bean));
}
运行后结果如下:
======JsonHierarchicalStreamDriver====JavaObject >>>> JaonString=========
-------Object >>>> JSON---------
{"student": {
"id": 1,
"name": "jack",
"email": "jack@email.com",
"address": "china",
"birthday": {
"birthday": "2010-11-22"
}
}}
{
"id": 1,
"name": "jack",
"email": "jack@email.com",
"address": "china",
"birthday": {
"birthday": "2010-11-22"
}
}
使用JsonHierarchicalStreamDriver转换默认会给转换后的对象添加一个根节点,但是在构建JsonHierarchicalStreamDriver驱动的时候,你可以重写createWriter方法,删掉根节点。
看上面的结果,一个是默认带根节点的JSON对象,它只是将类名作为一个属性,将对象作为该属性的一个值。而另一个没有带根属性的JSON就是通过重写createWriter方法完成的。
3、 将List集合转换成JSON字符串
@Test
public void writeList2JSON() {
failRed("======JsonHierarchicalStreamDriver====JavaObject >>>> JaonString=========");
JsonHierarchicalStreamDriver driver = new JsonHierarchicalStreamDriver();
xstream = new XStream(driver);
//xstream = new XStream(new JettisonMappedXmlDriver());//转换错误
//xstream.setMode(XStream.NO_REFERENCES);
xstream.alias("student", Student.class);
List<Student> list = new ArrayList<Student>();
list.add(bean);//add
bean = new Student();
bean.setAddress("china");
bean.setEmail("tom@125.com");
bean.setId(2);
bean.setName("tom");
Birthday day = new Birthday("2010-11-22");
bean.setBirthday(day);
list.add(bean);//add
bean = new Student();
bean.setName("jack");
list.add(bean);//add
fail(xstream.toXML(list));
//failRed("========JsonHierarchicalStreamDriver==删除根节点=========");
//删除根节点
xstream = new XStream(new JsonHierarchicalStreamDriver() {
public HierarchicalStreamWriter createWriter(Writer out) {
return new JsonWriter(out, JsonWriter.DROP_ROOT_MODE);
}
});
xstream.alias("student", Student.class);
fail(xstream.toXML(list));
}
运行后结果如下
======JsonHierarchicalStreamDriver====JavaObject >>>> JaonString=========
{"list": [{
"id": 1,
"name": "jack",
"email": "jack@email.com",
"address": "china",
"birthday": {
"birthday": "2010-11-22"
}
},
{
"id": 2,
"name": "tom",
"email": "tom@125.com",
"address": "china",
"birthday": {
"birthday": "2010-11-22"
}
},
{
"id": 0,
"name": "jack"
}
]}
{
"id": 1,
"name": "jack",
"email": "jack@email.com",
"address": "china",
"birthday": {
"birthday": "2010-11-22"
}
},
{
"id": 2,
"name": "tom",
"email": "tom@125.com",
"address": "china",
"birthday": {
"birthday": "2010-11-22"
}
},
{
"id": 0,
"name": "jack"
}
]
上面的list1是使用JsonHierarchicalStreamDriver 转换的,当然你也可以使用JettisonMappedXmlDriver驱动进行转换;用JettisonMappedXmlDriver转换后,你会发现格式不同而且没有根属性。
4、 Map转换json
@Test
public void writeMap2JSON() {
failRed("======JsonHierarchicalStreamDriver==== Map >>>> JaonString=========");
xstream = new XStream(new JsonHierarchicalStreamDriver());
//xstream = new XStream(new JettisonMappedXmlDriver());
xstream.alias("student", Student.class);
Map<String, Student> map = new HashMap<String, Student>();
map.put("No.1", bean);//put
bean = new Student();
bean.setAddress("china");
bean.setEmail("tom@125.com");
bean.setId(2);
bean.setName("tom");
bean.setBirthday(new Birthday("2010-11-21"));
map.put("No.2", bean);//put
bean = new Student();
bean.setName("jack");
map.put("No.3", bean);//put
fail(xstream.toXML(map));
//failRed("========JsonHierarchicalStreamDriver==删除根节点=========");
//删除根节点
xstream = new XStream(new JsonHierarchicalStreamDriver() {
public HierarchicalStreamWriter createWriter(Writer out) {
return new JsonWriter(out, JsonWriter.DROP_ROOT_MODE);
}
});
xstream.alias("student", Student.class);
fail(xstream.toXML(map));
}
运行后结果如下:
======JsonHierarchicalStreamDriver==== Map >>>> JaonString=========
{"map": [
[
"No.3",
{
"id": 0,
"name": "jack"
}
],
[
"No.1",
{
"id": 1,
"name": "jack",
"email": "jack@email.com",
"address": "china",
"birthday": {
"birthday": "2010-11-22"
}
}
],
[
"No.2",
{
"id": 2,
"name": "tom",
"email": "tom@125.com",
"address": "china",
"birthday": {
"birthday": "2010-11-21"
}
}
]
]}
[
[
"No.3",
{
"id": 0,
"name": "jack"
}
],
[
"No.1",
{
"id": 1,
"name": "jack",
"email": "jack@email.com",
"address": "china",
"birthday": {
"birthday": "2010-11-22"
}
}
],
[
"No.2",
{
"id": 2,
"name": "tom",
"email": "tom@125.com",
"address": "china",
"birthday": {
"birthday": "2010-11-21"
}
}
]
]
5、 将JSON转换java对象
/**
- <b>function:</b>JsonHierarchicalStreamDriver可以将简单的json字符串转换成java对象,list、map转换不成功;
- JsonHierarchicalStreamDriver读取JSON字符串到java对象出错
- @author hoojo
- @createDate Nov 27, 2010 1:22:26 PM
-
@throws JSONException
*/
@Test
public void readJSON2Object() throws JSONException {
String json = "{\"student\": {" +
"\"id\": 1," +
"\"name\": \"haha\"," +
"\"email\": \"email\"," +
"\"address\": \"address\"," +
"\"birthday\": {" +
"\"birthday\": \"2010-11-22\"" +
"}" +
"}}";
//JsonHierarchicalStreamDriver读取JSON字符串到java对象出错,但JettisonMappedXmlDriver可以
xstream = new XStream(new JettisonMappedXmlDriver());
xstream.alias("student", Student.class);
fail(xstream.fromXML(json).toString());//JettisonMappedXmlDriver转换List集合出错,但JsonHierarchicalStreamDriver可以转换正确
//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"]}}
json = "{\"list\": [{" +
"\"id\": 1," +
"\"name\": \"haha\"," +
"\"email\": \"email\"," +
"\"address\": \"address\"," +
"\"birthday\": {" +
"\"birthday\": \"2010-11-22\"" +
"}" +
"},{" +
"\"id\": 2," +
"\"name\": \"tom\"," +
"\"email\": \"tom@125.com\"," +
"\"address\": \"china\"," +
"\"birthday\": {" +
"\"birthday\": \"2010-11-22\"" +
"}" +
"}]}";
System.out.println(json);//用js转换成功
List list = (List) xstream.fromXML(json);
System.out.println(list.size());//0好像转换失败
}
运行后结果如下:
haha#1#address#2010-11-22#email
{"list": [{"id": 1,"name": "haha","email": "email","address": "address","birthday": {"birthday": "2010-11-22"}},
{"id": 2,"name": "tom","email": "tom@125.com","address": "china","birthday": {"birthday": "2010-11-22"}}]}
0
JSON到Java的转换是fromXML方法。
xstream很强,bean,xml,json,map之间可以相互转换。不过json还是fastjson方便,有涉及到xml还是xstream方便
共同学习,写下你的评论
评论加载中...
作者其他优质文章