package org.abin.lee.json;
public class Employee {
private String name="abin";
private double salary=1000;
private String sex="M";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
package org.abin.lee.json;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class JsonTest {
public static void main(String[] args) {
GsonBuilder builder=new GsonBuilder();
Gson gson=builder.create();
String json1=gson.toJson(new Employee());
System.out.println("json1="+json1);
Employee json2=gson.fromJson(json1, Employee.class);
System.out.println("json2="+json2);
System.out.println(json2.getName());
System.out.println(json2.getSalary());
System.out.println(json2.getSex());
System.out.println(json2.getClass());
}
}