码迷,mamicode.com
首页 > 编程语言 > 详细

java 序列化与反序列化

时间:2015-11-25 13:20:22      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:

类: 必须是单独的类文件

public class Student implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private long id ;
private String name;
private String age;

public Student(){     //必须有这个构造函数
}

public Student(int id,String name,String age){
this.id=id;
this.name=name;
this.age=age;
}

public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}

public String toString(){
return this.id + ", " + this.name + ", " + this.age;
}

 

Student studenttemp=new Student(1,"0","2");  //实例化对象
JSONObject json = JSONObject.fromObject(studenttemp); // 转换成json
Student stu= (Student)JSONObject.toBean(json, Student.class);   //反序列化;
System.out.println(stu);

 

 String strjson="{\"id\":1,\"name\":\"zhang\",\"age\":\"22\"}";

JSONObject json = JSONObject.fromObject(studenttemp);  //字符串转换成json;

Student stu= (Student)JSONObject.toBean(json, Student.class);   //反序列化对象;
System.out.println(stu);

java 序列化与反序列化

标签:

原文地址:http://www.cnblogs.com/zlabby/p/4994254.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!