/* */ public String toString() /* */ { /* 120 */ StringBuffer sb = new StringBuffer("{"); /* 121 */ int i = 0; /* 122 */ for (Map.Entry entry : this.objects.entrySet()) { /* 123 */ JSONInfo info = (JSONInfo) entry.getValue(); /* 124 */ String jsonPropertyName = (String) entry.getKey(); /* 125 */ if (info == null) { /* */ continue; /* */ } /* 128 */ if (i > 0) /* 129 */ sb.append(","); /* 130 */ i++; /* */ /* 132 */ sb.append("\"").append(jsonPropertyName).append("\""); /* 133 */ sb.append(":"); /* */ /* 135 */ Object obj = info.getObj(); /* */ /* 137 */ if (((obj instanceof String)) || ((obj instanceof Date)) || ((obj instanceof Timestamp))) { /* 138 */ sb.append("\""); /* */ } /* 140 */ if ((obj instanceof Collection)) /* 141 */ sb.append( BeanUtil.getCollection2JSON((Collection) obj, null, info.getProperties(), false)); /* 142 */ else if ((obj instanceof BaseObject)) /* 143 */ sb.append(BeanUtil.getBean2JSON(obj, info.getProperties())); /* 144 */ else if ((obj instanceof Date)) /* 145 */ sb.append(StringUtils.DateToStr((Date) obj, "yyyy-MM-dd")); /* 146 */ else if ((obj instanceof Timestamp)) /* 147 */ sb.append( StringUtils.DateToStr(new Date(((Timestamp) obj).getTime()), "yyyy-MM-dd hh:mm:ss")); /* */ else { /* 149 */ sb.append(obj.toString()); /* */ } /* 151 */ if (((obj instanceof String)) || ((obj instanceof Date)) || ((obj instanceof Timestamp))) { /* 152 */ sb.append("\""); /* */ } /* */ } /* 155 */ sb.append("}"); /* 156 */ return sb.toString(); /* */ }
/** * 提取集合中的对象的属性,组合成List. * * @param collection 来源集合. * @param propertityName 要提取的属性名. */ @SuppressWarnings("unchecked") public static List<Object> fetchElementPropertyToList( final Collection collection, final String propertyName) throws Exception { List<Object> list = new ArrayList<Object>(); for (Object obj : collection) { list.add(BeanUtil.getProperty(obj, propertyName)); } return list; }
/** @param args */ public static void main(String[] args) { // TODO Auto-generated method stub User u1 = new User("zhangsan", "123456", 30); System.out.println(BeanUtil.getValueByPropertyName(u1, "username")); System.out.println(BeanUtil.getValueByPropertyName(u1, "userpass")); }