gson使用教程-翻译6

来源:互联网 发布:Linux修改jvm内存大小 编辑:程序博客网 时间:2024/05/16 11:58

原文地址:http://www.studytrails.com/java/json/java-google-json-type-adapter.jsp

Using Custom Type Adapter
在之前的教程中我們已经见到如何序列化和反序列化有继承或者没有继承关系的Java类.Gson默认使用内省来序列化和反序列化.但是,有些时候,我们需要自定义的转换策略来取代默认的转换策略.所谓的转换策略就是控制如何将一个java对象转换成json字符串以及json字符串转换成java对象.Gson提供了指定转换策略的方法.
让我们看看如何去实现转换策略:
首先写一个继承TypeAdapter的类,然后实现 public abstract T read(JsonReader in) throws IOException;和public abstract void write(JsonWriter out, T value) throws IOException;方法.这个Adapter记得处理null哦.通过GsonBuilder来注册这个自定义的Adapter.看demo:

package com.studytrails.json.gson;import java.io.IOException;import java.net.MalformedURLException;import java.net.URL;import org.apache.commons.io.IOUtils;import com.google.gson.Gson;import com.google.gson.GsonBuilder;public class DatasetTypeAdapterExample8 {    public static void main(String[] args) throws MalformedURLException, IOException {        String url = "http://freemusicarchive.org/api/get/albums.json?api_key=60BLHNQCAOUFPIBZ&limit=5";        String json = IOUtils.toString(new URL(url));        // Create the custom type adapter and register it with the GsonBuilder        // class.        Gson gson = new GsonBuilder().registerTypeAdapter(Dataset.class, new DatasetTypeAdapter()).create();        // deserialize the json to Albums class. The Dataset objects are part of        // the Albums class. Whenever Gson encounters an object of type DataSet        // it calls the DatasetTypeAdapter to read and write json.        Albums albums = gson.fromJson(json, Albums.class);        System.out.println(albums.getDataset()[1].getAlbum_title());        // prints        // http://freemusicarchive.org/music/The_Yes_Sirs/Through_The_Cracks_Mix_Vol_1/    }}

自定义的Adapter

package com.studytrails.json.gson;import java.io.IOException;import com.google.gson.TypeAdapter;import com.google.gson.stream.JsonReader;import com.google.gson.stream.JsonToken;import com.google.gson.stream.JsonWriter;/** * The Dataset class contains the information about a particular Album. * album_title and album_url are two distinct fields in the json. The Dataset * object contains the field album_title. Normally Gson would map the * album_title property in the json the the album_title field in the Dataset * object. However, we dont want that. We want to use the album_url property * from the json object to populate the album_title field in the Dataset object. * we build a custom TypeAdapter to do that. This is just a trivial case, you * could also combine album_url and album_title properties and set it to the * album_title field of the Dataset Object. * */public class DatasetTypeAdapter extends TypeAdapter<Dataset> {    @Override    public Dataset read(JsonReader reader) throws IOException {        // the first token is the start object        JsonToken token = reader.peek();        Dataset dataset = new Dataset();        if (token.equals(JsonToken.BEGIN_OBJECT)) {            reader.beginObject();            while (!reader.peek().equals(JsonToken.END_OBJECT)) {                if (reader.peek().equals(JsonToken.NAME)) {                    if (reader.nextName().equals("album_url"))                        dataset.setAlbum_title(reader.nextString());                    else                        reader.skipValue();                }            }            reader.endObject();        }        return dataset;    }    @Override    public void write(JsonWriter out, Dataset value) throws IOException {    }}

Albums类

package com.studytrails.json.gson;public class Albums {    private String title;    private Dataset[] dataset;    public void setTitle(String title) {        this.title = title;    }    public void setDataset(Dataset[] dataset) {        this.dataset = dataset;    }    public String getTitle() {        return title;    }    public Dataset[] getDataset() {        return dataset;    }}

Dataset类

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);    }}
0 0
原创粉丝点击