package com.cjonline.foundation.util;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
public class JsonUtils {
/** 默认的字符串格式 */
private static String dateformat = "yyyy-MM-dd hh:mm:ss";
/**
* 获取日期字符串格式
*
* @return
*/
public static String getDateformat() {
return dateformat;
}
/**
* 设置日期字符串格式
*
* @param dateformat
*/
public void setDateformat(String dateformat) {
JsonUtils.dateformat = dateformat;
}
/**
* 获取实体bean的属性返回类型
*
* @param typeName
* 类型名称
* @param fieldValue
* 字段值
* @return
*/
private static Object toType(Object fieldValue) {
Object result = "";
if (fieldValue instanceof String) {
String value = (String) fieldValue;
if (value.contains("\r\n")) {
value = value.replaceAll("\r\n", "\\\\r\\\\n");
}
result = "\"" + value + "\"";
} else if (fieldValue instanceof Number) {
result = fieldValue;
} else if (fieldValue instanceof Boolean) {
result = fieldValue;
} else if (fieldValue instanceof BigDecimal) {
result = fieldValue;
} else if (fieldValue instanceof Date) {
SimpleDateFormat sdf = new SimpleDateFormat(getDateformat());
result = "\"" + sdf.format(fieldValue) + "\"";
} else {
result = "\"" + "\"";
;
}
return result;
}
/**
* 是将单个实体bean的格式化为json字符串
*
* @param obj
* 实体bean
* @return json字符串
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws Exception
*/
public static String Object2JSON(Object obj, Object[] showfields) {
StringBuffer sb = new StringBuffer();
sb.append("{");
if (obj == null) {
return sb.append("}").toString();
}
Field[] fds = obj.getClass().getDeclaredFields();
for (int i = 0; i < fds.length; i++) {
fds[i].setAccessible(true);
String fieldName = fds[i].getName();
Object fieldValue = null;
try {
fieldValue = fds[i].get(obj);
} catch (Exception e) {
e.printStackTrace();
}
if (showfields == null) {
sb.append("\"" + fieldName + "\"").append(":");
sb.append(toType(fieldValue)).append(",");
} else {
for (Object showfield : showfields) {
if (showfield instanceof String) {
if (fieldName.equalsIgnoreCase((String) showfield)) {
sb.append("\"" + fieldName + "\"").append(":");
sb.append(toType(fieldValue)).append(",");
}
}
}
}
}
String result = "";
if (sb.toString().length() == 1) {
result = "{";
} else {
result = sb.substring(0, sb.length() - 1);
}
return result + "}";
}
/** * 可以对多个实体bean的集合操作,输出的是grid的格式 * * @param obj * 可以使实体bean的集合或个体 * @param showfields * 需要显示的字段 * @return * @throws Exception */ public static String ListObject2JSON(Object obj, Object[] showfields) throws Exception { StringBuffer sb = new StringBuffer(); StringBuffer rows = new StringBuffer(); sb.append("["); if (obj instanceof Collection) { @SuppressWarnings("rawtypes") Collection cc = (Collection) obj; if (cc.size() < 1) { return sb.append("]").toString(); } Object[] objects = cc.toArray(); for (Object object : objects) { rows.append(Object2JSON(object, showfields)).append(","); } rows = rows.replace(rows.length() - 1, rows.length(), ""); } sb.append(rows).append("]"); return sb.toString(); } }