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