原文地址:http://www.studytrails.com/java/json/java-google-json-serialize-inner-classes.jsp
Gson可以解析内部类和静态内部类.下面的demo中给我们展示了以下的内容:
- 序列化包含静态内部类的类
- 序列化包含内部类的类
- 反序列化json为一个包含静态内部类和内部类的类
- 序列化静态内部类(不是封闭类型)
- 序列化非静态内部类(不是封闭类型)
- 反序列化json为一个静态内部类(不是封闭类型)
- 反序列化json为一个非静态内部类((不是封闭类型))
Demo:
package com.studytrails.json.gson;
import java.lang.reflect.Modifier;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.studytrails.json.gson.AlbumsWithInnerClass.Dataset;
import com.studytrails.json.gson.AlbumsWithInnerClass.Dataset2;
public class SerializeInnerClassExample4 {
public static void main(String[] args) {
// 添加数据
AlbumsWithInnerClass albums = new AlbumsWithInnerClass();
albums.setName("SerializeInnerClass");
albums.setYear("1988");
Dataset dataset = albums.new Dataset();
dataset.setAlbum_id("1");
dataset.setAlbum_name("albums1");
albums.setDatasetsInner(new Dataset[] { dataset });
Dataset2 dataset2 = new Dataset2();
dataset2.setAlbum_id("2");
dataset2.setAlbum_name("albums2");
albums.setDatasetsStatic(new Dataset2[] { dataset2 });
// create the GsonBuilder把对象转换成对应的json
GsonBuilder builder = new GsonBuilder();
// we ignore Private fields 忽略掉private修饰的字段
builder.excludeFieldsWithModifiers(Modifier.PRIVATE);
Gson gson = builder.create();
// serialize the albums object
String json = gson.toJson(albums);
System.out.println(json);
// 打印的结果:
// {"name":"SerializeInnerClass","datasetsInner":[{"album_name":"SerializeInnerClass_albums1","album_id":"1"}],
// "datasetsStatic":[{"album_name":"albums2","album_id":"2"}]}
// We read the json string now and recreate the AlbumsWithInnerClass
// class反序列化上面生成的json
Gson gson3 = new Gson();
AlbumsWithInnerClass parsedAlbums = gson3.fromJson(json,
AlbumsWithInnerClass.class);
System.out.println(parsedAlbums.datasetsInner[0].album_name);
// 打印的结果:SerializeInnerClass_albums1
System.out.println(parsedAlbums.datasetsStatic[0].album_name);
// 打印的结果:albums2
// now lets try and serialize only the object of inner class 仅仅序列化内部类
Gson gson2 = new Gson();
String json2 = gson2.toJson(dataset);
System.out.println(json2);
// 打印的结果: {"album_name":"SerializeInnerClass_albums1","album_id":"1"}
// serialize nested static class序列化静态内部类
String json3 = gson2.toJson(dataset2);
System.out.println(json3);
// 打印的结果: {"album_name":"albums2","album_id":"2"}
// let us now create the inner class from the json string反序列化内部类
Gson gson4 = new Gson();
Dataset parsedDataset = gson4.fromJson(json2, Dataset.class);
System.out.println(parsedDataset.getClass());
// 打印的结果: class com.studytrails.json.gson.AlbumsWithInnerClass$Dataset
System.out.println(parsedDataset.album_name);
// 打印的结果:SerializeInnerClass_albums1
// create nested static class from the json string反序列化静态内部类
Dataset2 parsedStaticNestedClass = gson4
.fromJson(json3, Dataset2.class);
System.out.println(parsedStaticNestedClass.getClass());
// 打印的结果: class com.studytrails.json.gson.AlbumsWithInnerClass$Dataset2
System.out.println(parsedStaticNestedClass.album_name);
// 打印的结果: albums2
}
}
AlbumsWithInnerClass類的代碼
package com.studytrails.json.gson;
public class AlbumsWithInnerClass {
public String name;
private String year;
public Dataset[] datasetsInner;
public Dataset2[] datasetsStatic;
public void setDatasetsInner(Dataset[] datasetsInner) {
this.datasetsInner = datasetsInner;
}
public void setDatasetsStatic(Dataset2[] datasetsStatic) {
this.datasetsStatic = datasetsStatic;
}
public void setName(String name) {
this.name = name;
}
public void setYear(String year) {
this.year = year;
}
class Dataset {
public String album_name;
public String album_id;
public void setAlbum_id(String album_id) {
this.album_id = album_id;
}
public void setAlbum_name(String album_name) {
this.album_name = name + "_" + album_name;
}
}
static class Dataset2 {
public String album_name;
public String album_id;
public void setAlbum_id(String album_id) {
this.album_id = album_id;
}
public void setAlbum_name(String album_name) {
this.album_name = album_name;
}
}
}