* 谷歌开原项目GitHub地址:https://github.com/google/gson
1.新建一个maven项目,引入gson的dependency坐标依赖以及commons-io的坐标:
<!--处理json格式数据-->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.4</version>
</dependency>
<!--做io文件的操作-->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
2.编写一个Student 的JavaBean:
package com.wxd.basic.gson;
import java.util.Arrays;
public class Student {
private String name;
private Integer age;
private String birthday;
private String[] major;
private boolean has_girl_friend;
private String car;
private String house;
private String comment;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String[] getMajor() {
return major;
}
public void setMajor(String[] major) {
this.major = major;
}
public boolean isHas_girl_friend() {
return has_girl_friend;
}
public void setHas_girl_friend(boolean has_girl_friend) {
this.has_girl_friend = has_girl_friend;
}
public String getCar() {
return car;
}
public void setCar(String car) {
this.car = car;
}
public String getHouse() {
return house;
}
public void setHouse(String house) {
this.house = house;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", birthday='" + birthday + '\'' +
", major=" + Arrays.toString(major) +
", has_girl_friend=" + has_girl_friend +
", car='" + car + '\'' +
", house='" + house + '\'' +
", comment='" + comment + '\'' +
'}';
}
}
3.编写GsonSample解析类
package com.wxd.basic.gson;
import com.google.gson.FieldNamingStrategy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.lang.reflect.Field;
public class GsonSample {
public static void main(String[] args){
Student s=new Student();
s.setName("张三");
s.setAge(23);
s.setBirthday("1995-06-30");
s.setMajor(new String[]{"打球","看书"});
s.setHas_girl_friend(false);
s.setCar(null);
s.setHouse(null);
s.setComment("这是一个注释!");
GsonBuilder gsonBuilder=new GsonBuilder(); //gsonBuilder.setDateFormat("yyyy-MM-dd").create();日期格式转换
gsonBuilder.setPrettyPrinting();//这样设置后通过GsonBuilder构建的Gson对象打印出来是美化过的json格式数据
gsonBuilder.setFieldNamingStrategy(new FieldNamingStrategy() {
@Override
public String translateName(Field field) {
if(field.getName().equals("name")){
return "NAME";//修改了name属性
}
return field.getName();
}
});
Gson gson=gsonBuilder.create();
// Gson gson=new Gson();
System.out.println(gson.toJson(s));
}
}
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格式数据:
package com.wxd.basic.gson;
import com.google.gson.Gson;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
public class GsonReadSample {
public static void main(String[] args){
File file=new File(GsonReadSample.class.getResource("/Student.json").getFile());
try {
String content = FileUtils.readFileToString(file);//如果不知道怎么读文件可以自己手动写个json字符串如下(把这行注释掉,下面的放开就行):
// String content="{\n" +
// " \"NAME\": \"张三\",\n" +
// " \"age\": 23,\n" +
// " \"birthday\": \"1995-06-30\",\n" +
// " \"major\": [\n" +
// " \"打球\",\n" +
// " \"看书\"\n" +
// " ],\n" +
// " \"has_girl_friend\": false,\n" +
// " \"comment\": \"这是一个注释!\"\n" +
// "}";
Gson gson=new Gson();
Student student = gson.fromJson(content, Student.class);
System.out.println(student);
} catch (IOException e) {
e.printStackTrace();
}
}
}
6.控制台输出:
Student{name='null', age=23, birthday='1995-06-30', major=[打球, 看书], has_girl_friend=false, car='null', house='null', comment='这是一个注释!'}