Assign a default value to JavaBean and go to the JSON string

Source: Internet
Author: User
Tags tojson


Software Testing Technology Exchange Group: 429183023


Today in the work of finishing Dubbo interface, some interface parameter types are JavaBean type of data, in the collation of the direct JavaBean parameters directly to the default JSON format collation, so think of how to JavaBean the default value directly into JSON-formatted data, avoiding the manual spelling of the JSON string. (This code also has to optimize the place, temporarily only supports the Java basic data type conversion, does not support the JavaBean to contain other JavaBean object variable conversion, also does not support list/map and so on type conversion, later has the time to find a way to optimize, If you have optimized code can also be taken out to share) nonsense not much to say, directly on the code

Package com.up.util.tools;

Import Com.google.gson.Gson;
Import Java.lang.reflect.Field;
Import Java.lang.reflect.Method;
Import Java.util.Date;
Import Java.util.HashMap;

Import Java.util.Map;
 /** * Created by * * 2017-3-29 * 11:07. */public class Javabeantojson {public static String Tojson (class Clazz) throws Exception {Gson Gson = new G
        Son ();
        String result = "";
        if (Clazz.equals (Map.class)) {result = Clazz.getname () + "\ T" + Gson.tojson (new hashmap<> ());
            else {object = Clazz.newinstance ();
            Object = Getbean (object);
        result = Clazz.getname () + "T" + gson.tojson (object);
    return result; private static <T> object Getbean (T object) throws Exception {class<?> ClassType = object.getc
        Lass ();
        SqlRowSet SRS = jdbctemplate.queryforrowset (SQL); field[] fields = Classtype.getdeclaredfields ()//Get the field in the object//each timeWhen looping, instantiate an object T objectcopy = (t) classtype.getconstructor (new class[]{}) that is the same as the object type passed in. newinstance (new object[]{});
            for (int i = 0; i < fields.length i++) {Field field = Fields[i];
            String fieldName = Field.getname ();
            Object value = null;
            Determines, based on the field type, which get method is used in the result set to fetch data from the data if (Field.gettype (). Equals (String.class)) {value = ""; else if (Field.gettype (). Equals (Int.class) | | | Field.gettype (). Equals (Integer.class)) {value = NE
            W Integer (0); else if (Field.gettype (). Equals (Double.class) | | | Field.gettype (). Equals (Double.class)) {value = new Dou
            BLE (0); else if (Field.gettype (). Equals (Long.class) | | | Field.gettype (). Equals (Long.class)) {value = new Long (0)
            ;
            else if (Field.gettype (). Equals (Date.class)) {value = new Date ();
else if (Field.gettype (). Equals (Object.class)) {                Value = new Object ();
            //Gets the first letter of the property and converts it to uppercase, corresponding to the setxxx String firstletter = fieldname.substring (0, 1). toUpperCase ();
            String setmethodname = "Set" + Firstletter + fieldname.substring (1);
            if (Fieldname.equals ("Serialversionuid")) {continue;
            Method Setmethod = Classtype.getmethod (Setmethodname, New Class[]{field.gettype ()});
    Setmethod.invoke (objectcopy, New Object[]{value});//Calling the object's Setxxx method} return objectcopy;
 }
}


Related Article

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.