编辑特别推荐:
利用Java实现网页浏览器
java函数参数传递
Java语言的接口与类型安全
Java代码
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import com.hysoft.commons.test.User;
/************************************************************************
* JSON工具类
*
* @author cobra
* @see com.hysoft.commons.xml.JSONHelper
* @since jdk1.6
* @version version1.0
* @date 2010/09/03
*
************************************************************************/
public class JSONHelper {
/**
* 传入一个简单的List经过解析返回JSON
*
* @param list 传入参数list
*
* @return String 返回解析过后的JSON格式例如[{"abc"},{"edf"},{"xyz"}]
*/
public static String getJSONByList(List list) {
StringBuffer buffer = new StringBuffer();
buffer.append("[{");
for (int i = 0; i < list.size(); i++) {
buffer.append("\"").append(list.get(i) != null ? list.get(i).toString() : "").append("\",");
}
return buffer.substring(0, buffer.length() - 1).concat("}]");
}
/**
* 传入一个Map经过解析返回JSON
*
* @param map 传入参数map
*
* @return String 返回解析过后的JSON格式例如[{"age","24","name","ty"}]
*/
@SuppressWarnings("unchecked")
public static String getJSONByMap(Map map) {
Iterator iter = map.entrySet().iterator();
StringBuffer buffer = new StringBuffer();
buffer.append("[{");
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
buffer.append("\"").append(entry.getKey() != null ? entry.getKey() : "").append("\",").append("\"").append(entry.getValue() != null ? entry.getValue() : "").append("\",");
}
return buffer.substring(0, buffer.length() - 1).concat("}]");
}
/**
* 传入一个List经过解析返回JSON
*
* @param list 传入参数list
*
* @return String 返回解析过后的JSON格式例如[{"age","24","name","ty"},{"age","20","name","tt"}]
*/
public static String getJSONByBeanList(List list) {
StringBuffer buffer = new StringBuffer();
try {
Class clazz = list.get(0).getClass();
Field fields[] = clazz.getDeclaredFields();
buffer.append("[");
for (int i = 0; i < list.size(); i++) {
buffer.append("{");
for (int j = 0; j < fields.length; j++) {
String fieldName = fields[j].getName();
String firstLetter = fieldName.substring(0, 1).toUpperCase(); // 获得和属性对应的getXXX()方法的名字
String getMethodName = "get" + firstLetter + fieldName.substring(1); // 获得和属性对应的getXXX()方法的名字
Method getMethod = clazz.getMethod(getMethodName, new Class[] {}); // 获得和属性对应的getXXX()方法
Object value = getMethod.invoke(list.get(i), new Object[] {});
buffer.append("\"" + fieldName + "\":\"" + value + "\",");
}
buffer = new StringBuffer(buffer.substring(0, buffer.length()-1));
buffer.append("},");
}
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return buffer.substring(0, buffer.length() - 1).concat("]");
}
来源:考试大-Java认证责编:xxm
相关推荐