zoukankan      html  css  js  c++  java
  • 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="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;
        }
    }
    

      

      

  • 相关阅读:
    HDU 6071
    HDU 6073
    HDU 2124 Repair the Wall(贪心)
    HDU 2037 今年暑假不AC(贪心)
    HDU 1257 最少拦截系统(贪心)
    HDU 1789 Doing Homework again(贪心)
    HDU 1009 FatMouse' Trade(贪心)
    HDU 2216 Game III(BFS)
    HDU 1509 Windows Message Queue(队列)
    HDU 1081 To The Max(动态规划)
  • 原文地址:https://www.cnblogs.com/byteworld/p/5923197.html
Copyright © 2011-2022 走看看