Encapsulating a JavaBean as a JSON object by reflection

Source: Internet
Author: User
Tags object object

Package Maskedmen.uitils.classparameterstojson;


Import Java.lang.reflect.Field;

Import Java.lang.reflect.Method;


/**

* @author Maskedmen

* Convert a class to a JSON object, that is, the properties of the encapsulated class

* Because this tool class largely destroys the encapsulation characteristics of a class

* Therefore, please be careful when using, and make appropriate changes if necessary.

*/

public class Classparameterstojson {


public static void Main (string[] args) {

TestClass TestClass = new TestClass ("Potato", "watermelon");

try {

Classparameterstojson (TestClass);

} catch (Exception e) {

E.printstacktrace ();

}

}

public static void Classparameterstojson (Object object) throws exception{

Class<?> clazz = Object.getclass ();//Get Class object

String paramname=null;//Parameter name

String paramtype=null;//Parameter type

String MethodName = null;//Method Name

Method Method=null;

Object Value=null;

int count=0;//record number of cycles

int length=0;

StringBuffer JSONBF = new StringBuffer ();

StringBuffer PARAMBF = new StringBuffer ();

field[] Parameters = Clazz.getdeclaredfields ();//Gets the parameter array of the class

length = Parameters.length;

Jsonbf.append ("{");

for (Field parameter:parameters) {//foreach loop

ParamName = Parameter.getname ();//Get parameter name

Paramtype = Parameter.gettype (). toString ();//Get parameter type

if (!paramtype.equals ("boolean")) {//Out of non-boolean type

if (count>=1&&count<length) {

Jsonbf.append (",");

}

Jsonbf.append ("\" "+paramname+" \ "");

Capitalize the first letter and connect the Get

MethodName = Parambf.append ("Get"). Append (paramname.substring (0, 1). toUpperCase ())

. Append (paramname.substring (1)). ToString ();

method = Clazz.getdeclaredmethod (MethodName, New class[]{});//Get corresponding methods according to the method name

if (!method.isaccessible ()) {//If private property

Method.setaccessible (TRUE);//set to visible

}

Value = Method.invoke (object, New object[]{});//executes the corresponding GET method

Value=value.tostring ();

Jsonbf.append (":");

Jsonbf.append ("\" "+value+" \ "");

count++;

Parambf.delete (0, Parambf.length ());//Empty every cycle

}else {

System.out.println ("Boolean type does not handle");

}

}

Jsonbf.append ("}");

System.out.println ("JSON string" +jsonbf.tostring ());

}

}



Test class

Class testclass{

Private String potato;//Potatoes

Private String watermelon;//Watermelon

Public TestClass () {

}

Public TestClass (string potato, string watermelon) {

Super ();

This.potato = potato;

This.watermelon = watermelon;

}


Public String Getpotato () {

return potato;

}


public void Setpotato (String potato) {

This.potato = potato;

}


Public String Getwatermelon () {

return watermelon;

}


public void Setwatermelon (String watermelon) {

This.watermelon = watermelon;

}

}

SOURCE Download Link Http://pan.baidu.com/s/1geAGaLP

This article is from the "maskedmen" blog, make sure to keep this source http://maskedmen.blog.51cto.com/8100866/1767575

Encapsulating a JavaBean as a JSON object by reflection

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.