原文地址:http://www.studytrails.com/java/json/java-google-json-serializing-collections.jsp
Serializing list
將Colloections轉成json就跟對象轉成json一樣.但是,問題在於Collections是可以有泛型的.所以在我們進行解析的時候需要將泛型的類型傳進對應的方法裏面.需要注意的是如果Collection中有不一樣的類型,那麼將無法進行解析.
以下就是示例代碼:
>() {}.getType();
List datasets = gson.fromJson(json, datasetListType);
for (Dataset dataset : datasets) {
System.out.println(dataset.getAlbum_title());
System.out.println(dataset.getAlbum_id());
}
// Prints
//album1
//1
//album2
//2
}
}" data-snippet-id="ext.8f9d091ade9603c7228cea570561db37" data-snippet-saved="false" data-csrftoken="wxInFRZY-upUb_4SBj56fFTapdh99tBVPdGs" data-codota-status="done">package com.studytrails.json.gson;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class DeSerializeListExample5 {
public static void main(String[] args) {
String json = "[{album_id:1,album_title:'album1'},{album_id:2,album_title:'album2'}]";
Gson gson = new Gson();
// create the type for the collection. In this case define that the collection is of type Dataset
Type datasetListType = new TypeToken<Collection<Dataset>>() {}.getType();
List<Dataset> datasets = gson.fromJson(json, datasetListType);
for (Dataset dataset : datasets) {
System.out.println(dataset.getAlbum_title());
System.out.println(dataset.getAlbum_id());
}
// Prints
//album1
//1
//album2
//2
}
}
DataSet類:
otherProperties = new HashMap();
public String getAlbum_id() {
return album_id;
}
public void setAlbum_id(String album_id) {
this.album_id = album_id;
}
public String getAlbum_title() {
return album_title;
}
public void setAlbum_title(String album_title) {
this.album_title = album_title;
}
public Object get(String name) {
return otherProperties.get(name);
}
}" data-snippet-id="ext.be7a5a225823cc78e314e602aa3fd029" data-snippet-saved="false" data-csrftoken="02dpYMPM-Ty4ZHIBj3_ITwM1fXe5AlfzlKCA" data-codota-status="done">package com.studytrails.json.gson;
import java.util.HashMap;
import java.util.Map;
public class Dataset {
private String album_id;
private String album_title;
private Map<String , Object> otherProperties = new HashMap<String , Object>();
public String getAlbum_id() {
return album_id;
}
public void setAlbum_id(String album_id) {
this.album_id = album_id;
}
public String getAlbum_title() {
return album_title;
}
public void setAlbum_title(String album_title) {
this.album_title = album_title;
}
public Object get(String name) {
return otherProperties.get(name);
}
}
同樣的在一些使用泛型的普通java類型面對同樣的問題.Gsont提供一個TypeToken的類來保存泛型的類型信息下面的demo中展示瞭如何使用TypeToken這個類來轉換和解析有泛型的普通類.
animal = new Animal();
// Create a Dog instance
Dog dog = new Dog("I am a dog");
animal.setAnimal(dog);
Gson gson = new Gson();
// Define a Type that is an Animal of type dog.
Type animalType = new TypeToken>() {
}.getType();
// we first convert the animal object to a json and then read the json
// back. However we define the json to be of Animal type
Animal animal1 = gson.fromJson(gson.toJson(animal, animalType), Animal.class);
System.out.println(animal1.get().getClass()); // prints class
// com.google.gson.internal.LinkedTreeMap
// In contrast to above where we read the json back using the Animal
// type, here we read the json back as the custom animalType Type. This
// gives Gson an idea of what
// the generic type should be.
Animal animal2 = gson.fromJson(gson.toJson(animal), animalType);
System.out.println(animal2.get().getClass());
// prints class com.studytrails.json.gson.Dog
}
}" data-snippet-id="ext.5763325dc80adb719d96612af9843b1f" data-snippet-saved="false" data-csrftoken="whIW2m9G-MB4Cy6cV1Ike3o7K_4kXf7h9kaI" data-codota-status="done">package com.studytrails.json.gson;
import java.lang.reflect.Type;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class GenericTypesExample8 {
public static void main(String[] args) {
// create an animal class that is of type dog.
Animal<dog> animal = new Animal<Dog>();
// Create a Dog instance
Dog dog = new Dog("I am a dog");
animal.setAnimal(dog);
Gson gson = new Gson();
// Define a Type that is an Animal of type dog.
Type animalType = new TypeToken<Animal<Dog>>() {
}.getType();
// we first convert the animal object to a json and then read the json
// back. However we define the json to be of Animal type
Animal animal1 = gson.fromJson(gson.toJson(animal, animalType), Animal.class);
System.out.println(animal1.get().getClass()); // prints class
// com.google.gson.internal.LinkedTreeMap
// In contrast to above where we read the json back using the Animal
// type, here we read the json back as the custom animalType Type. This
// gives Gson an idea of what
// the generic type should be.
Animal animal2 = gson.fromJson(gson.toJson(animal), animalType);
System.out.println(animal2.get().getClass());
// prints class com.studytrails.json.gson.Dog
}
}
Animal類:
package com.studytrails.json.gson;
public class Animal<t> {
public T animal;
public void setAnimal(T animal) {
this.animal = animal;
}
public T get() {
return animal;
}
}
Dog類:
package com.studytrails.json.gson;
public class Dog {
private String name;
public Dog(String name) {
this.name = name;
}
public String getName() {
return name;
}
}