1-1.Diaosi.java(bean)
package Bean; import com.google.gson.annotations.SerializedName; public class Diaosi { @SerializedName("NAME") private String name; private String school; private boolean has_girlfriend; private double age; private Object car; private String[] major; private transient String ignore; public String getIgnore() { return ignore; } public void setIgnore(String ignore) { this.ignore = ignore; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSchool() { return school; } public void setSchool(String school) { this.school = school; } public boolean isHas_girlfriend() { return has_girlfriend; } public void setHas_girlfriend(boolean has_girlfriend) { this.has_girlfriend = has_girlfriend; } public double getAge() { return age; } public void setAge(double age) { this.age = age; } public Object getCar() { return car; } public void setCar(Object car) { this.car = car; } public String[] getMajor() { return major; } public void setMajor(String[] major) { this.major = major; } public String getComment() { return comment; } public void setComment(String comment) { this.comment = comment; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } private String comment; private String birthday; // public String toString(){ // // return "[name:'"+name+"',school:'"+school+"',has_girlfriend:'"+has_girlfriend+"',age:'"+age+"',major:'"+major+"']"; // } }
1-2.
GsonGreateSample.java
package day05.gson; import java.lang.reflect.Field; import com.google.gson.FieldNamingStrategy; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import Bean.Diaosi; public class GsonGreateSample { public static void main(String[] args) { Diaosi wangxiaoer = new Diaosi(); Object nullObject = null; wangxiaoer.setName("wangxiaoer"); wangxiaoer.setAge(25.2); wangxiaoer.setBirthday("1990-01-01"); wangxiaoer.setSchool("蓝翔"); wangxiaoer.setMajor(new String[] { "理发", "挖掘机" }); wangxiaoer.setHas_girlfriend(false); wangxiaoer.setCar(nullObject); wangxiaoer.setComment("这是一个注释"); // gson GsonBuilder gsonbuilder = new GsonBuilder(); gsonbuilder.setPrettyPrinting(); gsonbuilder.setFieldNamingStrategy(new FieldNamingStrategy() { @Override public String translateName(Field f) { if (f.getName().equals("school")) { return "SCHOOL"; } return f.getName(); } }); Gson gson = gsonbuilder.create(); System.out.println(gson.toJson(wangxiaoer)); } }
结果:
-------------------------------------------------------
{
"NAME": "wangxiaoer",
"SCHOOL": "蓝翔",
"has_girlfriend": false,
"age": 25.2,
"major": [
"理发",
"挖掘机"
],
"comment": "这是一个注释",
"birthday": "1990-01-01"
}
-------------------------------------------------------
2-1.
Diaosinew.java(bean)
package Bean; import java.util.List; import com.google.gson.annotations.SerializedName; public class Diaosinew { @SerializedName("NAME") private String name; private String school; private boolean has_girlfriend; private double age; private Object car; private List<String> major; public List<String> getMajor() { return major; } public void setMajor(List<String> major) { this.major = major; } private transient String ignore; public String getIgnore() { return ignore; } public void setIgnore(String ignore) { this.ignore = ignore; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSchool() { return school; } public void setSchool(String school) { this.school = school; } public boolean isHas_girlfriend() { return has_girlfriend; } public void setHas_girlfriend(boolean has_girlfriend) { this.has_girlfriend = has_girlfriend; } public double getAge() { return age; } public void setAge(double age) { this.age = age; } public Object getCar() { return car; } public void setCar(Object car) { this.car = car; } public String getComment() { return comment; } public void setComment(String comment) { this.comment = comment; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } private String comment; private String birthday; // public String toString(){ // // return "[name:'"+name+"',school:'"+school+"',has_girlfriend:'"+has_girlfriend+"',age:'"+age+"',major:'"+major+"']"; // } }
2-2.
ReadGSONSample.java
package day05; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import com.google.gson.Gson; import Bean.Diaosinew; public class ReadGSONSample { public static void main(String[] args) throws IOException { File file = new File(ReadJSONSample.class.getResource("/wangxiaoer.json").getFile()); String content = FileUtils.readFileToString(file); Gson gson=new Gson(); Diaosinew wangxiaoer =(Diaosinew) gson.fromJson(content, Diaosinew.class); System.out.println(wangxiaoer.getMajor()); System.out.println(wangxiaoer.getMajor().getClass()); } }
结果:
[理发, 挖掘机]
class java.util.ArrayList
既往不恋,未来不迎,当下不杂