Java序列化与反序列化三种格式存取
作者:网络转载 发布时间:[ 2012/8/14 10:16:17 ] 推荐标签:
输出结果:
<Person-array>
<Person>
<name>Jay</name>
<age>24</age>
</Person>
<Person>
<name>Tom</name>
<age>23</age>
</Person>
</Person-array>
3、JSON格式
//参考:http://www.cnblogs.com/hoojo/archive/2011/04/22/2025197.html
public class SerializeJSON {
public static void main(String[] args) {
SerializeJSON serializeJSON = new SerializeJSON();
serializeJSON.writeJSON();
serializeJSON.readJSON();
}
public void writeJSON(){
XStream xStream = new XStream(new JettisonMappedXmlDriver());
Person person = new Person("geniushehe", 16);
try {
FileOutputStream fos = new FileOutputStream("E:\json.js");
xStream.setMode(XStream.NO_REFERENCES);
xStream.alias("Person", Person.class);
xStream.toXML(person, fos);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void readJSON(){
XStream xStream = new XStream(new JettisonMappedXmlDriver());
Person person = null;
try {
FileInputStream fis = new FileInputStream("E:\json.js");
xStream.setMode(XStream.NO_REFERENCES);
xStream.alias("Person", Person.class);
person = (Person)xStream.fromXML(fis);
System.out.println(person.getName());
System.out.println(person.getAge());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
输出结果:
{"Person":{"name":"geniushehe","age":16}}
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11