日期:2014-05-16  浏览次数:20547 次

自定义SenchaTouch数据JSON格式处理工具

?

package org.fanzone.util;

?

import java.beans.IntrospectionException;

import java.beans.Introspector;

import java.beans.PropertyDescriptor;

import java.io.File;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

import java.util.Set;

?

/**?

?* Title: Touch<br>?

?* Description: Base Handler Utilities<br>?

?* Copyright: Copyright (c) 2011 <br>?

?* Create DateTime: Jun 2, 2011 2:40:20 PM <br>?

?* @author wangmeng

?*/

public class SenchaUtil {

?

/**

* Appending object via string.

* @param object

* @return java.lang.String

*/

public static String objectToJson(Object object) {

StringBuilder json = new StringBuilder();

if (object == null) {

json.append("\"\"");

}else if (object instanceof String ) {

json.append("\"").append((String)object).append("\"");

}else if (object instanceof Long){

json.append("\"").append((Long)object).append("\"");

}else if (object instanceof Integer){

json.append("\"").append((Integer)object).append("\"");

}else if (object instanceof Double){

json.append("\"").append((Double)object).append("\"");

}else if (object instanceof Float){

json.append("\"").append((Float)object).append("\"");

}else if (object instanceof Short){

json.append("\"").append((Short)object).append("\"");

}else if (object instanceof Character){

json.append("\"").append((Character)object).append("\"");

}else if (object instanceof Character){

json.append("\"").append((Character)object).append("\"");

}else {

json.append(beanToJson(object));

}

return json.toString();

}

?

/**

* Formating attributes.

* @param bean

* @return String

*/

public static String beanToJson(Object bean) {

StringBuilder json = new StringBuilder();

json.append("{");

PropertyDescriptor[] props = null;

try {

props = Introspector.getBeanInfo(bean.getClass(), Object.class).getPropertyDescriptors();