标签:
类: 必须是单独的类文件
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);
标签:
原文地址:http://www.cnblogs.com/zlabby/p/4994254.html