:报错
JSONClassNotFoundException: org.codehaus.jettison.mapped.Configuration
在构造JSON解析器的时候,使用JettisonMappedXmlDriver
,导包正常,但是一包报错,找不到,尴尬很久。最后发现,如果使用JettisonMappedXmlDriver
,需要jettison-1.x.x.jar 包,特此记录一下。
Student student = new Student("songbl", 666, "1");
//构造序列化JSON.设置JSON解析器
XStream xStreamJ = new XStream(new JsonHierarchicalStreamDriver());
xStreamJ.setMode(XStream.NO_REFERENCES);
//为类名节点重命名
xStreamJ.alias("stu", Student.class);
//Json序列化
String json = xStreamJ.toXML(student);
System.out.println(json);
PrintWriter pw = resp.getWriter();
pw.write(json);
![{
"stu": {
"name": "songbl",
"uid": 666,
"gender": "1"
}
}]