package com.common.util; import java.sql.Timestamp; import java.util.Date; import net.sf.json.JsonConfig; import net.sf.json.processors.JsonValueProcessor; /** * 自定义JsonValueProcessor * 比如我们要控制JSON序列化过程中的Date对象的格式化,以及数值的格式化,JsonValueProcessor是最好的选择。 * @author bitaotao * */ public class JsonDateValueProcessor implements JsonValueProcessor { public Object processArrayValue(Object value, JsonConfig config) { return process(value); } public Object processObjectValue(String key, Object value, JsonConfig config) { return process(value); } private Object process(Object value){ if(value instanceof Date){ return DateUtil.sdfyyyy_MM_dd_HH_mm_ss.format(value); }else if(value instanceof Timestamp){ return DateUtil.sdfyyyy_MM_dd_HH_mm_ss.format(value); } return value == null ? "" : value.toString(); } }
package com.common.util; import java.math.RoundingMode; import java.text.NumberFormat; import java.util.Locale; import net.sf.json.JsonConfig; import net.sf.json.processors.JsonValueProcessor; /** * 只对Float生效,对float不生效 * @author ctl * */ public class JsonFloatValueProcessor implements JsonValueProcessor { /** * 处理数组类型 */ public Object processArrayValue(Object value, JsonConfig jsonConfig) { if (value instanceof float[]) { String[] obj = {}; float[] nums = (float[]) value; for (int i = 0; i < nums.length; i++) { obj[i] = roundHalfUp(nums[i], 3); } return obj; } return value; } /** * 处理单个对象 */ public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) { if (value instanceof Float) { return roundHalfUp((Float)value, 3); } return value; } /** * 四舍五入。 * * @param number 数值 * @return 舍入后的数值 * @see java.text.RoundingMode.HALF_UP */ public String roundHalfUp(double number, int frac) { NumberFormat fmt = NumberFormat.getInstance(Locale.CHINA); fmt.setMaximumFractionDigits(frac); fmt.setRoundingMode(RoundingMode.HALF_UP); return fmt.format(number); } }
package com.common.util; import java.util.Date; public class Person{ int id; Integer max; Float price; float priceMax; Double jine; double jineMax; boolean success; Boolean result; String name; Date savetime; public int getId() { return id; } public void setId(int id) { this.id = id; } public Integer getMax() { return max; } public void setMax(Integer max) { this.max = max; } public Float getPrice() { return price; } public void setPrice(Float price) { this.price = price; } public float getPriceMax() { return priceMax; } public void setPriceMax(float priceMax) { this.priceMax = priceMax; } public Double getJine() { return jine; } public void setJine(Double jine) { this.jine = jine; } public double getJineMax() { return jineMax; } public void setJineMax(double jineMax) { this.jineMax = jineMax; } public boolean isSuccess() { return success; } public void setSuccess(boolean success) { this.success = success; } public Boolean getResult() { return result; } public void setResult(Boolean result) { this.result = result; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getSavetime() { return savetime; } public void setSavetime(Date savetime) { this.savetime = savetime; } }
package com.common.util; import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.Method; import java.sql.Timestamp; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.TreeMap; import net.sf.json.JSONObject; import net.sf.json.JsonConfig; import org.apache.log4j.Logger; /** *转换器 *1:将JavaBean 转换成Map、JSONObject *2:将JSONObject 转换成Map * * @author xxx */ public class BeanConverter { static Logger logger = Logger.getLogger(BeanConverter.class); public static void main(String[] args) throws Exception { JsonConfig jsonConfig=new JsonConfig(); Map<String,Object> classMap=new HashMap<String,Object>(); jsonConfig.setClassMap(classMap); JsonDateValueProcessor jsonValueProcessor = new JsonDateValueProcessor(); jsonConfig.registerJsonValueProcessor(Date.class, jsonValueProcessor); jsonConfig.registerJsonValueProcessor(java.sql.Date.class, jsonValueProcessor); jsonConfig.registerJsonValueProcessor(Timestamp.class, jsonValueProcessor); jsonConfig.registerJsonValueProcessor(Float.class, new JsonFloatValueProcessor()); Person person=new Person(); person.setId(1); person.setJine(999.34d); person.setJineMax(9.12d); person.setMax(10); person.setName("ctl"); person.setPrice(1000.99f); person.setPriceMax(10.01f); person.setResult(true); person.setSavetime(new Date()); person.setSuccess(false); Map<String, Object> data =new TreeMap<String, Object>(); transBean2Map(person, data); Person personData=new Person(); System.out.println(data); transMap2Bean(personData, data); System.out.println(JSONObject.fromObject(personData,jsonConfig)); } /** * 将json对象转换成Map * * @param jsonObject json对象 * @return Map对象 */ @SuppressWarnings("unchecked") public static Map<Object, Object> toMap(JSONObject jsonObject) { Map<Object, Object> result = new HashMap<Object, Object>(); Iterator<Object> iterator = jsonObject.keys(); Object key = null; Object value = null; while (iterator.hasNext()) { key = iterator.next(); value = jsonObject.get(key); result.put(key, value); } return result; } /** * 将javaBean转换成JSONObject * * @param bean javaBean * @return json对象 */ // public static JSONObject toJSON(Object bean) // { // return new JSONObject(toMap(bean)); // } /** * 将map转换成Javabean * @param javabean javaBean * @param data map数据 */ public static Object toJavaBean(Object javabean, Map<Object, Object> data) { Method[] methods = javabean.getClass().getDeclaredMethods(); for (Method method : methods){ String field =null; try { if (method.getName().startsWith("set")){ field = method.getName(); field = field.substring(field.indexOf("set") + 3); field = field.toLowerCase().charAt(0) + field.substring(1); Object value=data.get(field); if(value!=null){ try { method.invoke(javabean, new Object[]{(Object)value}); } catch (Exception e) { method.invoke(javabean, new Object[]{Float.parseFloat(value.toString())}); } } } }catch (Exception e){ logger.error(field+"类型为:"+field.getClass(),e); } } return javabean; } public static Map<String, Object> transBean2Map(Object obj,Map<String,Object> map) { if(obj == null){ return null; } try { BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor property : propertyDescriptors) { String key = property.getName(); // 过滤class属性 if (!key.equals("class")) { // 得到property对应的getter方法 Method getter = property.getReadMethod(); Object value = getter.invoke(obj); if(null!=value&&!"".equals(value)) map.put(key, value); } } } catch (Exception e) { logger.error("transBean2Map Error " , e); } return map; } public static Object transMap2Bean(Object javabean, Map<String, Object> data) { Method[] methods = javabean.getClass().getDeclaredMethods(); for (Method method : methods){ try{ if (method.getName().startsWith("set")){ String field = method.getName(); field = field.substring(field.indexOf("set") + 3); field = field.toLowerCase().charAt(0) + field.substring(1); method.invoke(javabean, new Object[]{data.get(field)}); } }catch (Exception e){ logger.error(e); } } return javabean; } /** * 将javaBean转换成JSONObject * * @param bean javaBean * @return json对象 * @throws ParseException json解析异常 */ // public static void toJavaBean(Object javabean, String data) throws ParseException // { // JSONObject jsonObject = new JSONObject(data); // Map<String, String> datas = toMap(jsonObject); // toJavaBean(javabean, datas); // } }