java反射 创建对象_使用Java反射自动生成一个未知对象所有属性的json语句

public static List getListAjaxString(List

listParam){

List result = new ArrayList();

if(listParam.size()>0)

for(int

i=0;i

StringBuffer s= new StringBuffer("{");

Class c = listParam.get(i).getClass();

Field[] fields = c.getDeclaredFields();

String itemString = null;

for (Field f : fields) {

try {

//Object itemObject = f.get(listParam.get(i));

Object itemObject =

invokeGetMethod(c,f.getName(),listParam.get(i));

s.append ("""+ f.getName() +"":"" +

dispString(itemObject)+"",");

itemString = s.toString().substring(0, s.length()-1)

+ "}";

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

result.add(itemString);

}

return result;

}

private static Object

invokeGetMethod(Class> c, String

filedName, Object obj)

throws SecurityException,

NoSuchMethodException, IllegalArgumentException,

IllegalAccessException,

InvocationTargetException {

Method method =

assembleGetMethod(c, filedName);

Object[] nullObjects = null;

return method.invoke(obj,

nullObjects);

}

private static Method

assembleGetMethod(Class> c, String

filedName)  throws SecurityException,

NoSuchMethodException {

String

methodName = "get" + upperFirst(filedName);

Class>[]

nullClasses = null;

return

c.getDeclaredMethod(methodName, nullClasses);

}

private static String upperFirst(String

sparam){

return

sparam.toUpperCase().substring(0,1)+sparam.substring(1);

}

private static String dispString(Object param){

String result = null;

if (param

instanceof Integer) {

int value =

((Integer) param).intValue();

result =

DataType.toDispString(value);

} else if (param

instanceof String) {

String s =

(String) param;

result =

DataType.toDispString(s);

} else if (param

instanceof Double) {

double d =

((Double) param).doubleValue();

result =

DataType.toDispString(d);

} else if (param

instanceof Float) {

float f =

((Float) param).floatValue();

result =

DataType.toDispString(f);

} else if (param

instanceof Long) {

long l =

((Long) param).longValue();

result =

DataType.toDispString(l);

} else if (param

instanceof Boolean) {

boolean b =

((Boolean) param).booleanValue();

//result =

DataType.toDispString(b);

} else if (param

instanceof Date) {

Date d = (Date)

param;

result =

DataType.toDispString(d);

}

return

result;

}

public static void main(String[] args){

List l = new ArrayList();

Operate su = new Operate();

Operate su1 = new Operate();

su.setDescription("abc");

su.setOperateId( new BigDecimal(123));

su.setOperateName("hanjin1");

su1.setDescription("abcd");

su1.setOperateId( new BigDecimal(234));

su1.setOperateName("hanjin2");

l.add(su);

l.add(su1);

List result = getListAjaxString(l);

for(Object item : result){

System.out.print(item);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值