Gson的使用

来源:互联网 发布:学习软件大全下载 编辑:程序博客网 时间:2024/05/18 21:51

转载自http://www.cnblogs.com/Dentist/p/Mr_Dentist.html

简单例子:

public class Person {    private String name;    private int age;        public Person(String name,int age){        this.name=name;        this.age=age;    }}

1、gson的序列化:对象与String之间的转换

<span style="white-space:pre"></span>Person p=new Person("jack",18);        Gson gson=new Gson();        String json=gson.toJson(p);        Log.i(TAG, json);
输出结果: {"name":"jack","age":18}

对象数组的也是自动转换

 <span style="white-space:pre"></span>p=new ArrayList<Person>();        p.add(new Person("jack",18));        p.add(new Person("alex",23));        p.add(new Person("jane",15));        Gson gson=new Gson();        String json=gson.toJson(p);        Log.i(TAG, json);
输出结果: [{"name":"jack","age":18},{"name":"alex","age":23},{"name":"jane","age":15}]


 2、Gson的反序列化,String与对象的转换

jsonstr= {"name":"jack","age":18}

 <span style="white-space:pre"></span>Gson gson=new Gson();        Person person=gson.fromJson(jsonstr, Person.class);
jsonstr= [{"name":"jack","age":18},{"name":"alex","age":23},{"name":"jane","age":15}]
<span style="white-space:pre"></span>Gson gson=new Gson();        Person[] person1=gson.fromJson(jsonstr, Person[].class);

  对于数组类可以直接转。但对于集合类就必须要用TypeToken。它是gson提供的数据类型转换器,可以支持各种数据集合类型转换。

<span style="white-space:pre"></span>Gson gson=new Gson();        ArrayList<Person> person2=gson.fromJson(jsonstr, new TypeToken<ArrayList<Person>>(){}.getType());
 还要说一下如果对象的某个值为null。这个属性将不会在String中显示。
        ArrayList<Person> p=new ArrayList<Person>();        p.add(new Person("jack",18));        p.add(new Person(null,23));        p.add(new Person("jane",15));        Gson gson=new Gson();        String json=gson.toJson(p);        Log.i(TAG, json);

 输出结果:[{"name":"jack","age":18},{"age":23},{"name":"jane","age":15}]

  如果要被序列化的String缺少某个属性例如上面的输出。也会转换成功。并且对应值为null。


 3.Gson序列化的类屏蔽

  比如你想将一个类的几个属性序列化,但他继承自一个很复杂的父类。你不想序列化他的父类。

  又或者你在反序列化一个类时,因为复杂的父类而序列化失败。

  使用这个类。屏蔽父类或属性类成员不应序列化的类。

public class SpecificClassExclusionStrategy implements ExclusionStrategy {    private final Class<?> excludedThisClass;    private final Class<?> excludedThisClassFields;    /***     * 过滤器初始化     *     * @param excludedThisClass     *            该类和继承自该类的对象实例将被忽略     * @param excluedThisClassFields     *            该类的属性将不被序列化     */    public SpecificClassExclusionStrategy(Class<?> excludedThisClass, Class<?> excluedThisClassFields) {        this.excludedThisClass = excludedThisClass;        this.excludedThisClassFields = excluedThisClassFields;    }    @Override    public boolean shouldSkipClass(Class<?> clazz) {        if (clazz == null) return false;        if (clazz.equals(excludedThisClass)) return true;        return shouldSkipClass(clazz.getSuperclass());    }    @Override    public boolean shouldSkipField(FieldAttributes f) {        return f.getDeclaringClass().equals(excludedThisClassFields);    }}
Gson gson2 = new GsonBuilder().setExclusionStrategies(new SpecificClassExclusionStrategy(null, Model.class)).create();PersonBrief.PersonBriefResult result = gson2.fromJson(response, PersonBrief.PersonBriefResult.class);

  还有一个在对象与JsonObject直接转的。但这个JsonObject不是Android的JSONObject所以很少用。

 <span style="white-space:pre"></span>Person p=new Person("jack",18);        Gson gson=new Gson();        JsonElement jsonele=gson.toJsonTree(p);        JsonObject json=jsonele.getAsJsonObject();<span style="white-space:pre"></span>







0 0