首页 > 代码库 > Gson手动序列化POJO(工具类)

Gson手动序列化POJO(工具类)

gson2.7版本

只是简单的工具类(练习所用):

package pojo;import javax.xml.bind.annotation.XmlSeeAlso;import com.google.gson.annotations.SerializedName;public class User {    public String name;    public int age;    @SerializedName(value="http://www.mamicode.com/email_Address",alternate={"email"})    public String emailAddress;    public User() {    }    public User(String name, int age, String emailAddress) {        super();        this.name = name;        this.age = age;        this.emailAddress = emailAddress;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    public String getEmailAddress() {        return emailAddress;    }    public void setEmailAddress(String emailAddress) {        this.emailAddress = emailAddress;    }    @Override    public String toString() {        return "User [name=" + name + ", age=" + age + ", emailAddress=" + emailAddress + "]";    }        }
package utils;import java.io.IOException;import java.io.StringReader;import java.lang.reflect.Field;import com.google.gson.Gson;import com.google.gson.stream.JsonReader;import pojo.User;/** * 处理json工具类 * @Description: * @author zbs * @date 2016年9月30日 上午10:06:05 */public class JsonUtils {    public static void main(String[] args) {        String json="{\"name\":\"学学习\",\"age\":\"24\"}";        User user = (User) getObjectFromJson(User.class, json);        System.out.println(user);    }    /**     * json-->pojo     * @Description:序列化json     * @param clazz     * @param json     * @return void 返回类型     */    @SuppressWarnings("all")    public static  Object getObjectFromJson(Class clazz, String json) {        JsonReader reader = new JsonReader(new StringReader(json));        Field[] field = clazz.getDeclaredFields();        Object obj = null;        try {            // 获取当前对象            obj = clazz.newInstance();            reader.beginObject();            while (reader.hasNext()) {                String name = reader.nextName();                for (Field f : field) {                    if (f.getName().equals(name)) {                       if (f.getType() == int.class) {                            f.setInt(obj, reader.nextInt());                        }else if(f.getType() == Double.class){                            f.setDouble(obj, reader.nextDouble());                        }else if (f.getType() == Long.class) {                            f.setLong(obj, reader.nextLong());                        }else if (f.getType() == Boolean.class) {                            f.setBoolean(obj, reader.nextBoolean());                        }else{                            f.set(obj, reader.nextString());                         }                    }                }            }            reader.endObject();        } catch (Exception e) {            e.printStackTrace();        }        return obj;    }}

  

  

Gson手动序列化POJO(工具类)