import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
public class JSON1 {
private static final String quote = "\"";
private static final String braceL = "{";
private static final String braceR = "}";
private static final String middleBraceL = "[";
private static final String middleBraceR = "]";
private static final String comma = ",";
private static final String colon = ":";
private static final String defaultDateFormat = "yyyy-MM-dd HH:mm:ss";
public static String toJsonString(Object obj) {
return toJsonString(obj, defaultDateFormat);
}
public static String toJsonString(Object obj, String dateFormat) {
StringBuilder sb = null;
try {
sb = parseObject(obj, dateFormat);
} catch (IntrospectionException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
if (null != sb && sb.length() > 0 && sb.substring(sb.length() - 1).equals(comma)) {
sb = new StringBuilder(sb.substring(0, sb.lastIndexOf(comma)));
}
return sb == null ? "" : sb.toString();
}
public static StringBuilder parseObject(Object object, String dateFormat) throws IntrospectionException, InvocationTargetException, IllegalAccessException {
StringBuilder sb = new StringBuilder();
if (canGetValue(object)) {
sb.append(getSimpleValue(object, dateFormat));
} else if (object instanceof List) {
//遇到list
sb.append(middleBraceL);
List list = (List) object;
for (Object o : list) {
sb.append(parseObject(o, dateFormat)).append(comma);
}
if (list.size() > 0) {
sb = new StringBuilder(sb.substring(0, sb.lastIndexOf(comma)));
}
sb.append(middleBraceR);
} else if (object instanceof Map) {
//遇到map
sb.append(braceL);
Map map = (Map) object;
Set
for (Map.Entry entry : entrySet) {
sb.append(quote).append(entry.getKey()).append(quote).append(colon);
sb.append(parseObject(entry.getValue(), dateFormat)).append(comma);
}
if (map.size() > 0) {
sb = new StringBuilder(sb.substring(0, sb.lastIndexOf(comma)));
}
sb.append(braceR);
} else if (object.getClass().isArray()) {
//遇到数组
sb.append(middleBraceL);
List list = Arrays.asList(object);
for (Object o : list) {
sb.append(parseObject(o, dateFormat)).append(comma);
}
if (list.size() > 0) {
sb = new StringBuilder(sb.substring(0, sb.lastIndexOf(comma)));
}
sb.append(middleBraceR);
} else {
//其他正常plain对象
BeanInfo beanInfo = Introspector.getBeanInfo(object.getClass()); // 获取类属性
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
sb.append(braceL);
for (PropertyDescriptor pd : pds) {
if (pd.getName().equals("class")) {
continue;
}
String key = pd.getName();
Method readMethod = pd.getReadMethod();
Object result = readMethod.invoke(object, null);
sb.append(quote).append(key).append(quote).append(colon);
sb.append(parseObject(result, dateFormat)).append(comma);
}
if (pds.length > 1) {
sb = new StringBuilder(sb.substring(0, sb.lastIndexOf(comma)));
}
sb.append(braceR);
}
return sb;
}
private static StringBuilder getSimpleValue(Object val, String dateFormat) {
if (null == val) {
return new StringBuilder("null");
}
StringBuilder sb = new StringBuilder();
if (needQuote(val)) {
sb.append(quote);
if (val instanceof Date) {
SimpleDateFormat sf = new SimpleDateFormat(dateFormat);
sb.append(new StringBuilder(sf.format(val)));
} else {
sb.append(val);
}
sb.append(quote);
} else {
sb.append(val);
}
return sb;
}
private static boolean needQuote(Object object) {
return object instanceof String || object instanceof Date;
}
private static boolean canGetValue(Object object) {
return object instanceof Boolean || object instanceof Character ||
object instanceof Byte || object instanceof Short ||
object instanceof Integer || object instanceof Double ||
object instanceof Long || object instanceof Date ||
object instanceof BigDecimal || object instanceof Float ||
object instanceof String || null == object;
}
public static void main(String[] args) {
// String[] ss = {"sdf", "dfd"};
// System.out.println(ss.getClass().isArray());
User user = new User(20, "yujun", "男");
User subUser = new User(2, "yuxx", "女");
user.setSubUser(subUser);
List
User user1 = new User(1, "a", "n");
User user2 = new User(2, "b", "v");
User user3 = new User(3, "c", "n");
User user4 = new User(4, "d", "v");
user4.setSubUser(user);
users.add(user1);
users.add(user2);
users.add(user3);
users.add(user4);
Map
m.put("a", "dfsdf");
m.put("b", user);
m.put("c", users);
m.put("date", new Date());
//list
System.out.println(toJsonString(users));
//map
System.out.println(toJsonString(m));
//other obj
System.out.println(toJsonString(user));
}
}
class User {
private Integer age;
private String name;
private String sex;
private User subUser;
public User() {
}
public User(Integer age, String name, String sex) {
this.age = age;
this.name = name;
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public User getSubUser() {
return subUser;
}
public void setSubUser(User subUser) {
this.subUser = subUser;
}
}