JSONUtils
来源:互联网 发布:newsql数据库 编辑:程序博客网 时间:2024/04/29 23:02
package cn.net.zzfz.center.common.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class JSONUtils
{
public static SerializeConfig dateSerialize(String format)
{
SerializeConfig mapping = new SerializeConfig();
mapping.put(Date.class, new SimpleDateFormatSerializer(format));
return mapping;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Map<String, Object> object2Map(Object object)
{
Map<String, Object> returnMap = new HashMap<String, Object>();
if (object == null) {
return returnMap;
}
if(object instanceof Map) {
return (Map) object;
}
BeanInfo beanInfo = null;
try {
beanInfo = Introspector.getBeanInfo(object.getClass());
} catch (IntrospectionException e1) {
return returnMap;
}
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (int i = 0; i < propertyDescriptors.length; i++) {
try {
PropertyDescriptor descriptor = propertyDescriptors[i];
String propertyName = descriptor.getName();
if (!propertyName.equals("class")) {
Method readMethod = descriptor.getReadMethod();
Object result = readMethod.invoke(object, new Object[0]);
if (result != null)
returnMap.put(propertyName, result);
}
} catch (NullPointerException e) {
} catch (Exception e) {
break;
}
}
return returnMap;
}
public static String toJSONString(Object object, String dateFormat)
{
return JSON.toJSONString(object, dateSerialize(dateFormat), new SerializerFeature[0]);
}
}
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class JSONUtils
{
public static SerializeConfig dateSerialize(String format)
{
SerializeConfig mapping = new SerializeConfig();
mapping.put(Date.class, new SimpleDateFormatSerializer(format));
return mapping;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Map<String, Object> object2Map(Object object)
{
Map<String, Object> returnMap = new HashMap<String, Object>();
if (object == null) {
return returnMap;
}
if(object instanceof Map) {
return (Map) object;
}
BeanInfo beanInfo = null;
try {
beanInfo = Introspector.getBeanInfo(object.getClass());
} catch (IntrospectionException e1) {
return returnMap;
}
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (int i = 0; i < propertyDescriptors.length; i++) {
try {
PropertyDescriptor descriptor = propertyDescriptors[i];
String propertyName = descriptor.getName();
if (!propertyName.equals("class")) {
Method readMethod = descriptor.getReadMethod();
Object result = readMethod.invoke(object, new Object[0]);
if (result != null)
returnMap.put(propertyName, result);
}
} catch (NullPointerException e) {
} catch (Exception e) {
break;
}
}
return returnMap;
}
public static String toJSONString(Object object, String dateFormat)
{
return JSON.toJSONString(object, dateSerialize(dateFormat), new SerializerFeature[0]);
}
}
0 0
- JSONUtils
- JsonUtils
- JsonUtils
- JsonUtils
- JsonUtils
- JsonUtils
- JsonUtils
- JsonUtils
- JsonUtils
- JsonUtils.java
- JsonUtils.java
- 34、JsonUtils
- JsonUtils工具类
- JsonUtils & json转换
- JSONUtils 工具类
- 常用工具类之JsonUtils
- 工具类--JsonUtils 数据解析
- JsonUtils ~~fastJson解析工具类
- CsvUtil
- git linux diff
- 获取APP版本信息,可用于更新
- 日经春秋 20150318
- Tomcat在eclipse下无法访问或丢包
- JSONUtils
- 一些数据结构的应用
- jbpm4.4与tomcat6的jar包冲突
- 一树桃红,在生命的枝头葱茏
- 日经社説 20150318 国際秩序乱すロシアへの疑念
- IOS如何添加pch文件
- Tomcat Session机制,不及时释放导致内存溢出的性能问题分析
- 欢迎使用CSDN-markdown编辑器
- 软件开发与算法在计算机中的应用