Convert object object to map property name and value form

Source: Internet
Author: User

The form of a key-value pair that converts a Java object into a map

Code:

Package cn.lonelcoud.util;

Import Com.sun.deploy.util.StringUtils;
Import Java.lang.reflect.Field;
Import Java.text.SimpleDateFormat;

Import java.util.*;
 /** * Created by Lonecloud on 17/3/12. * For parsing object and converting it into map key value pairs * * @author Lonecloud * @version 1.0/public class Objectutils {private static F
    inal String javap = "java.";

    private static final String javadatestr = "Java.util.Date"; /** * Gets the value and name inside the class using reflection get * * @param obj * @return * @throws illegalaccessexception/Publ IC static map<string, object> objecttomap (Object obj) throws illegalaccessexception {map<string, object& Gt
        Map = new hashmap<> ();
        Class<?> clazz = Obj.getclass ();
        System.out.println (Clazz);
            For (Field Field:clazz.getDeclaredFields ()) {field.setaccessible (true);
            String fieldName = Field.getname ();
            Object value = field.get (obj); Map.put (FieldName, value);
    } return map; /** * Use recursive calls to get all of the values in Object * @param timeformatstr format time string default <strong>2017-03-10 10:21</s trong> * @param obj Object * @param excludefields excluded Properties * @return * @throws illegalaccessexce Ption */public static map<string, string> objecttomapstring (string timeformatstr, Object obj, string ... ex

        Cludefields) throws Illegalaccessexception {map<string, string> Map = new hashmap<> ();
            if (excludefields.length!=0) {list<string> List = arrays.aslist (excludefields);
        Objecttransfer (timeformatstr, obj, map, list);
        }else{Objecttransfer (timeformatstr, obj, map,null);
    } return map; /** * Recursive call function * @param obj Object * @param map map * @param Excludefield s corresponding parameter * @return * @throws illegalaccessexception/private static Map<sTring, string> objecttransfer (String timeformatstr, Object obj, map<string, string> Map, list<string>
        Excludefields) throws Illegalaccessexception {Boolean isexclude=false;
        Default string formatstr = "Yyyy-mm-dd HH:mm:ss"; 
        Sets the format string if (timeformatstr!= null &&!timeformatstr.isempty ()) {formatstr = Timeformatstr;
        } if (excludefields!=null) {isexclude=true;
        } class<?> Clazz = Obj.getclass ();  Gets the value for (Field field:clazz.getDeclaredFields ()) {String fieldName = clazz.getsimplename () + "." +
            Field.getname ();
            Determine if you need to skip a property if (Isexclude&&excludefields.contains (FieldName)) {continue;
            }//Setting property can be accessed field.setaccessible (true);
            Object value = field.get (obj);
            class<?> Valueclass = Value.getclass (); if (valueclass. Isprimitive ()) {Map.put (FieldName, value.tostring ()); else if (Valueclass.getname (). Contains (JAVAP)) {//Determine whether the base type if (Valueclass.getname (). Equals (JAVADATESTR)
                    {//Format date type SimpleDateFormat SDF = new SimpleDateFormat (FORMATSTR);
                    Date date = (date) value;
                    String datastr = Sdf.format (date);
                Map.put (FieldName, DATASTR);
                else {map.put (fieldName, value.tostring ());
            } else {Objecttransfer (timeformatstr, value, map,excludefields);
    } return map; }

}

The code is shared in the GitHub GitHub address

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.