為實體類增加toJSON方法

來源:互聯網
上載者:User

標籤:etl   build   his   pen   ret   getname   try   float   style   

後期子類繼承該基礎類即可。

public class BaseEntity implements Serializable {    /**     *      */    private static final long serialVersionUID = -5311594725961174392L;    @Override    public String toString() {        try {            return toJSON();        } catch (IllegalArgumentException e) {            e.printStackTrace();        } catch (IllegalAccessException e) {            e.printStackTrace();        }        return null;    }        public String toJSON() throws IllegalArgumentException, IllegalAccessException {        StringBuilder jsonBuff = new StringBuilder();        jsonBuff.append("{");        Field[] fields = this.getClass().getDeclaredFields();        for (Field item : fields) {            item.setAccessible(true);            String name = item.getName();            jsonBuff.append("\"" + name + "\"" + ":");            Object value = item.get(this);            if (value != null) {                Class<?> type = item.getType();                if (type.equals(String.class)) {                    jsonBuff.append("\"" + value + "\"");                } else if (type.equals(int.class) || type.equals(Integer.class)) {                    jsonBuff.append(item.getInt(this));                } else if (type.equals(short.class) || type.equals(Short.class)) {                    jsonBuff.append(item.getShort(this));                } else if (type.equals(long.class) || type.equals(Long.class)) {                    jsonBuff.append(item.getLong(this));                } else if (type.equals(float.class) || type.equals(Float.class)) {                    jsonBuff.append(item.getFloat(this));                } else if (type.equals(double.class) || type.equals(Double.class)) {                    jsonBuff.append(item.getDouble(this));                } else if (type.equals(char.class)) {                    jsonBuff.append(item.getChar(this));                } else if (type.equals(boolean.class)) {                    jsonBuff.append(item.getBoolean(this));                } else if (type.equals(Map.class)) {                    jsonBuff.append(mapToString((Map)value));                } else if (type.equals(List.class)) {                    jsonBuff.append(listToString((List)value));                } else if (type.getSuperclass().equals(BaseEntity.class)) {                    jsonBuff.append(((BaseEntity)value).toJSON());                }            }            jsonBuff.append(", ");        }        return jsonBuff.substring(0, jsonBuff.length() - 2) + "}";    }        public String mapToString(Map<String, ? extends Object> map) {        StringBuilder buff = new StringBuilder();        Iterator<String> iter = map.keySet().iterator();        buff.append("{");        while (iter.hasNext()) {            String name = iter.next();            Object value = map.get(name);            buff.append("\"" + name + "\"" + ":");            if (value != null) {                String valueStr = String.valueOf(value);                if (value.getClass().equals(String.class)) {                    buff.append("\"" + valueStr + "\"");                } else {                    buff.append(valueStr);                }            }            buff.append(", ");        }        return buff.substring(0, buff.length() - 2) + "}";    }        public String listToString(List<? extends Object> list) {        StringBuilder buff = new StringBuilder();        buff.append("[");        for (Object value : list) {            String valueStr = String.valueOf(value);            if (value.getClass().equals(String.class)) {                buff.append("\"" + valueStr + "\"");            } else {                buff.append(valueStr);            }            buff.append(", ");        }        return buff.substring(0, buff.length() - 2) + "]";    }}

 

為實體類增加toJSON方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.