java反射实现javabean转json实例代码
objecttojson.java
package com.objecttojson;
import java.lang.reflect.field;
import java.lang.reflect.invocationtargetexception;
import java.lang.reflect.method;
import java.util.arraylist;
import java.util.iterator;
public class objecttojson {
public static string objlisttojson(arraylist<?> list){
stringbuilder build=new stringbuilder();
//迭代器
iterator<?> iterator=list.iterator();
build.append("[");
string separate="";
while(iterator.hasnext()){
build.append(separate);
//object转json字符串
string str=objtojson(iterator.next());
build.append(str);
separate=",";
}
build.append("]");
return build.tostring();
}
@suppresswarnings("unchecked")
public static string objtojson(object obj){
stringbuilder build=new stringbuilder();
build.append("{");
@suppresswarnings("rawtypes")
class cla=null;
try {
//反射加载类
cla=class.forname(obj.getclass().getname());
} catch (classnotfoundexception e) {
system.out.println(obj.getclass().tostring().concat(" 未找到这个类"));
e.printstacktrace();
return null;
}
stringbuffer methodname=new stringbuffer();
//获取java类的变量
field[] fields=cla.getdeclaredfields();
string separate="";
for(field temp:fields){
build.append(separate);
build.append("\"");
build.append(temp.getname());
build.append("\":");
methodname.append("get");
methodname.append(temp.getname().substring(0,1).touppercase());
methodname.append(temp.getname().substring(1));
build.append("\"");
method method=null;
try {
//获取java的get方法
method=cla.getmethod(methodname.tostring());
} catch (nosuchmethodexception | securityexception e) {
methodname.setlength(0);
e.printstacktrace();
}
try {
//执行get方法,获取变量参数的直。
build.append(method.invoke(obj));
} catch (illegalaccessexception e) {
e.printstacktrace();
} catch (illegalargumentexception e) {
e.printstacktrace();
} catch (invocationtargetexception e) {
e.printstacktrace();
}
build.append("\"");
methodname.setlength(0);
separate=",";
}
build.append("}");
return build.tostring();
}
}
test.java
package com.test;
import java.util.arraylist;
import com.objecttojson.objecttojson;
public class test {
public static void main(string[] args){
//数据构建
arraylist<user> list=getlist();
//数据转换
string listjson=objecttojson.objlisttojson((arraylist<?>)list);
system.out.println("{\"data\":"+listjson+"}");
}
public static arraylist<user> getlist(){
user user00=new user();
user00.setuserage("27");
user00.setusername("smart");
user00.setusersex("man");
user user01=new user();
user01.setuserage("27");
user01.setusername("smart00");
user01.setusersex("man");
arraylist<user> list=new arraylist<user>();
list.add(user00);
list.add(user01);
return list;
}
}
user.java
package com.test;
public class user {
private string username;
private string usersex;
private string userage;
public string getusername() {
return username;
}
public void setusername(string username) {
this.username = username;
}
public string getusersex() {
return usersex;
}
public void setusersex(string usersex) {
this.usersex = usersex;
}
public string getuserage() {
return userage;
}
public void setuserage(string userage) {
this.userage = userage;
}
}
测试结果
{"data":[{"username":"smart","usersex":"man","userage":"27"},{"username":"smart00","usersex":"man","userage":"27"}]}
上一篇: java线程的基础实例解析
下一篇: swift 单例的实现方法及实例