* 谷歌开原项目GitHub地址:https://github.com/google/gson
1.新建一个maven项目,引入gson的dependency坐标依赖以及commons-io的坐标:
1<!--处理json格式数据--> 2 <dependency> 3 <groupId>com.google.code.gson</groupId> 4 <artifactId>gson</artifactId> 5 <version>2.4</version> 6 </dependency> 7 8 <!--做io文件的操作--> 9 <dependency> 10 <groupId>commons-io</groupId> 11 <artifactId>commons-io</artifactId> 12 <version>2.4</version> 13 </dependency>
2.编写一个Student 的JavaBean:
1package com.wxd.basic.gson; 2 3import java.util.Arrays; 4 5public class Student { 6 private String name; 7 private Integer age; 8 private String birthday; 9 private String[] major; 10 private boolean has_girl_friend; 11 private String car; 12 private String house; 13 private String comment; 14 15 public String getName() { 16 return name; 17 } 18 19 public void setName(String name) { 20 this.name = name; 21 } 22 23 public Integer getAge() { 24 return age; 25 } 26 27 public void setAge(Integer age) { 28 this.age = age; 29 } 30 31 public String getBirthday() { 32 return birthday; 33 } 34 35 public void setBirthday(String birthday) { 36 this.birthday = birthday; 37 } 38 39 public String[] getMajor() { 40 return major; 41 } 42 43 public void setMajor(String[] major) { 44 this.major = major; 45 } 46 47 public boolean isHas_girl_friend() { 48 return has_girl_friend; 49 } 50 51 public void setHas_girl_friend(boolean has_girl_friend) { 52 this.has_girl_friend = has_girl_friend; 53 } 54 55 public String getCar() { 56 return car; 57 } 58 59 public void setCar(String car) { 60 this.car = car; 61 } 62 63 public String getHouse() { 64 return house; 65 } 66 67 public void setHouse(String house) { 68 this.house = house; 69 } 70 71 public String getComment() { 72 return comment; 73 } 74 75 public void setComment(String comment) { 76 this.comment = comment; 77 } 78 79 @Override 80 public String toString() { 81 return "Student{" + 82 "name='" + name + '\'' + 83 ", age=" + age + 84 ", birthday='" + birthday + '\'' + 85 ", major=" + Arrays.toString(major) + 86 ", has_girl_friend=" + has_girl_friend + 87 ", car='" + car + '\'' + 88 ", house='" + house + '\'' + 89 ", comment='" + comment + '\'' + 90 '}'; 91 } 92}
3.编写GsonSample解析类
1package com.wxd.basic.gson; 2 3import com.google.gson.FieldNamingStrategy; 4import com.google.gson.Gson; 5import com.google.gson.GsonBuilder; 6 7import java.lang.reflect.Field; 8 9public class GsonSample { 10 public static void main(String[] args){ 11 Student s=new Student(); 12 s.setName("张三"); 13 s.setAge(23); 14 s.setBirthday("1995-06-30"); 15 s.setMajor(new String[]{"打球","看书"}); 16 s.setHas_girl_friend(false); 17 s.setCar(null); 18 s.setHouse(null); 19 s.setComment("这是一个注释!"); 20 21 GsonBuilder gsonBuilder=new GsonBuilder(); //gsonBuilder.setDateFormat("yyyy-MM-dd").create();日期格式转换 22 gsonBuilder.setPrettyPrinting();//这样设置后通过GsonBuilder构建的Gson对象打印出来是美化过的json格式数据 23 gsonBuilder.setFieldNamingStrategy(new FieldNamingStrategy() { 24 @Override 25 public String translateName(Field field) { 26 if(field.getName().equals("name")){ 27 return "NAME";//修改了name属性 28 } 29 return field.getName(); 30 } 31 }); 32 Gson gson=gsonBuilder.create(); 33// Gson gson=new Gson(); 34 System.out.println(gson.toJson(s)); 35 } 36}
4.控制台输出
①通过new Gson()的方式构建后的打印输出:{"name":"张三","age":23,"birthday":"1995-06-30","major":["打球","看书"],"has_girl_friend":false,"comment":"这是一个注释!"}②通过GsonBuilder的方式构建Gson对象后输出:
{
"NAME": "张三",
"age": 23,
"birthday": "1995-06-30",
"major": [
"打球",
"看书"
],
"has_girl_friend": false,
"comment": "这是一个注释!"
}
5.使用Gson解析json格式数据:
1package com.wxd.basic.gson; 2 3import com.google.gson.Gson; 4import org.apache.commons.io.FileUtils; 5 6import java.io.File; 7import java.io.IOException; 8 9public class GsonReadSample { 10 public static void main(String[] args){ 11 File file=new File(GsonReadSample.class.getResource("/Student.json").getFile()); 12 try { 13 String content = FileUtils.readFileToString(file);//如果不知道怎么读文件可以自己手动写个json字符串如下(把这行注释掉,下面的放开就行): 14// String content="{\n" + 15// " \"NAME\": \"张三\",\n" + 16// " \"age\": 23,\n" + 17// " \"birthday\": \"1995-06-30\",\n" + 18// " \"major\": [\n" + 19// " \"打球\",\n" + 20// " \"看书\"\n" + 21// " ],\n" + 22// " \"has_girl_friend\": false,\n" + 23// " \"comment\": \"这是一个注释!\"\n" + 24// "}"; 25 Gson gson=new Gson(); 26 Student student = gson.fromJson(content, Student.class); 27 System.out.println(student); 28 } catch (IOException e) { 29 e.printStackTrace(); 30 } 31 } 32}
6.控制台输出:
Student{name='null', age=23, birthday='1995-06-30', major=[打球, 看书], has_girl_friend=false, car='null', house='null', comment='这是一个注释!'}