在 build.gradle 中引入依赖:
implementation 'com.google.code.gson:gson:2.8.6'
compileOnly 'org.projectlombok:lombok:1.18.16'
annotationProcessor 'org.projectlombok:lombok:1.18.16'
示例:将对象序列化为json字符串
package com.example.gson;
import lombok.Data;
@Data
public class UserInfo {
private String name;
}
package com.example.gson;
import com.google.gson.Gson;
public class Main {
public static void main(String[] args) {
Gson gson = new Gson();
UserInfo userInfo = new UserInfo();
userInfo.setName("李白");
String jsonStr = gson.toJson(userInfo);
System.out.println(jsonStr);
}
}
执行 Main 类,运行结果:
{"name":"李白"}
示例:将 json 字符串反序列化为对象
package com.example.gson;
import com.google.gson.Gson;
import lombok.Data;
public class Main {
@Data
public static class UserInfo {
private String name;
}
public static void main(String[] args) {
Gson gson = new Gson();
String jsonStr = "{\"name\":\"李白\"}";
UserInfo userInfo = gson.fromJson(jsonStr, UserInfo.class);
System.out.println(userInfo);
}
}
运行结果:
Main.UserInfo(name=李白)
若 fromJson 方法的第一个参数为 null,无法编译通过,因为由多个 fromJson 函数符合条件。
若为空字符串或者 {}
呢?
package com.example.gson;
import com.google.gson.Gson;
import lombok.Data;
public class Main {
@Data
public static class UserInfo {
private String name;
}
public static void main(String[] args) {
Gson gson = new Gson();
System.out.println(gson.fromJson("", UserInfo.class));
System.out.println(gson.fromJson(" ", UserInfo.class));
System.out.println(gson.fromJson("{}", UserInfo.class));
System.out.println(gson.fromJson("{ } ", UserInfo.class));
System.out.println(gson.fromJson("{ \"age\": 18}", UserInfo.class));
}
}
运行结果:
null
null
Main.UserInfo(name=null)
Main.UserInfo(name=null)
Main.UserInfo(name=null)
示例:序列化数字、字符串
package com.example.gson;
import com.google.gson.Gson;
import lombok.Data;
public class Main {
public static void main(String[] args) {
Gson gson = new Gson();
System.out.println(gson.toJson("123"));
System.out.println(gson.toJson(123));
}
}
运行结果:
"123"
123
示例:反序列化数字、字符串
package com.example.gson;
import com.google.gson.Gson;
public class Main {
public static void main(String[] args) {
Gson gson = new Gson();
String str = gson.fromJson("\"123\"", String.class);
System.out.println(str);
str = gson.fromJson("123", String.class);
System.out.println(str);
Long num = gson.fromJson("\"123\"", Long.class);
System.out.println(num);
num = gson.fromJson("123", Long.class);
System.out.println(num);
}
}
执行结果:
123
123
123
123
示例:反序列化为 List<T>
package com.example.gson;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.util.List;
public class Main {
public static void main(String[] args) {
Gson gson = new Gson();
List<Integer> result = gson.fromJson("[1,2,3]", new TypeToken<List<Integer>>(){}.getType());
for (Integer item: result) {
System.out.println(item);
}
}
}
运行结果:
1
2
3
package com.example.gson;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.util.List;
public class Main {
public static void main(String[] args) {
Gson gson = new Gson();
List<Long> result = gson.fromJson("[1,2,3]", new TypeToken<List<Long>>(){}.getType());
for (Long item: result) {
System.out.println(item);
}
}
}
运行结果:
1
2
3
示例:POJO 中含有 List<T>
package com.example.gson;
import com.google.gson.Gson;
import lombok.Data;
import java.util.Arrays;
import java.util.List;
public class Main {
@Data
public static class UserInfo {
private String name;
private List<Long> scores;
}
public static void main(String[] args) {
UserInfo libai = new UserInfo();
libai.setName("李白");
libai.setScores(Arrays.asList(99L, 98L, 100L));
Gson gson = new Gson();
String jsonStr = gson.toJson(libai);
System.out.println(jsonStr);
UserInfo libai2 = gson.fromJson(jsonStr, UserInfo.class);
System.out.println(libai2);
for (Long score : libai2.scores) {
System.out.println(score);
}
}
}
执行结果:
{"name":"李白","scores":[99,98,100]}
Main.UserInfo(name=李白, scores=[99, 98, 100])
99
98
100
示例:POJO 中含有 byte
import com.google.gson.Gson;
import lombok.Data;
import java.io.Serializable;
public class Main {
@Data
public static class SimpleVO implements Serializable {
private Byte a;
private Byte[] b;
private byte c;
}
public static void main(String[] args) {
SimpleVO simpleVO = new SimpleVO();
simpleVO.setA((byte) 12);
simpleVO.setB(new Byte[] {1,2,3});
Gson gson = new Gson();
System.out.println(gson.toJson(simpleVO));
System.out.println(gson.fromJson(gson.toJson(simpleVO), SimpleVO.class));
}
}
执行结果:
{"a":12,"b":[1,2,3],"c":0}
Main.SimpleVO(a=12, b=[1, 2, 3], c=0)