日期:2014-05-16  浏览次数:20361 次

json学习所用。。。

木事做了一个json的小例子,大概还有很多的不到之处,先记录一下吧,大概过几天就又忘了。

@SuppressWarnings({ "rawtypes", "unchecked" })
public static  <T> String json11(Object ot) throws InstantiationException, IllegalAccessException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException, ClassNotFoundException{
	
	Class cls=ot.getClass();
	java.lang.reflect.Field[] file=cls.getDeclaredFields();
	 String json="";
	 for(int i =0;i<file.length;i++){
		String name= file[i].getName();
		 String name1="get"+file[i].getName().substring(0, 1).toUpperCase()+file[i].getName().substring(1, file[i].getName().length());
		 System.out.println(name1);
		Method getm=cls.getMethod(name1,new Class[]{});
		 json+=name+":"+getm.invoke(ot, new Class[]{})+"|";
		 
	 }
	 
	 System.out.println(json);
	 return json;
	
}

?