json-lib的toBean问题

XXBean有nested字段需要inject数据和有日期型数据

JsonConfig config = new JsonConfig();
config.setPropertySetStrategy(new PropertySetStrategy() {
	@Override
	public void setProperty(Object bean, String key, Object value)
			throws JSONException {
		try {
			PropertyUtils.setNestedProperty(bean, key, value);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
});
XXBean xx = new XXBean();
XXBeanPK pk = new XXBeanPK();
xx.setPk(pk);

// 需要将字段中的日期型数据由java.sql.Date/java.sql.Timestamp改为java.util.Date
JSONUtils.getMorpherRegistry().registerMorpher(
		new DateMorpher(new String[] { "dd/MM/yyyy", "HH:mm" }));

xx = (XX) JSONObject.toBean(JSONObject.fromObject(data, config), xx, config);
log.info("xx "+ ToStringBuilder.reflectionToString(xx, ToStringStyle.MULTI_LINE_STYLE));

你可能感兴趣的:(json-lib的toBean问题)