对FastJson的封装Util

/**

* FastJson常用帮助类

* @author huangxincheng

*

*/

public class FastJsonUtil {


private static final SerializeConfig CONfig = new SerializeConfig();

private static final String FORMAT_TIME = "yyyy-MM-dd HH:mm:ss";

private static final String FORMAT_DATE = "yyyy-MM-dd";

private static final SerializerFeature[] FEATURES = {

SerializerFeature.WriteMapNullValue,

SerializerFeature.WriteDateUseDateFormat,

SerializerFeature.WriteNullListAsEmpty

};

// WriteMapNullValue

// WriteDateUseDateFormat

static {

CONfig.put(Date.class,new SimpleDateFormatSerializer(FORMAT_TIME));

}

public static <T> T parSEObject(String json,Class<T> clazz) {

try {

T t = JSON.parSEObject(json,clazz);

return t;

} catch (Exception e) {

e.printstacktrace();

}

return null;

}

public static <T> List<T> parseList(String json,Class<T> clazz) {

try {

List<T> list = JSON.parseArray(json,clazz);

return list;

} catch (Exception e) {

e.printstacktrace();

}

return null;

}

/**

* 某人转为yyyy-MM-dd HH:mm:ss格式

* @param object

* @return

*/

public static String toJson(Object object) {

try {

String json = JSON.toJSONString(object,CONfig,FEATURES);

return json;

} catch (Exception e) {

e.printstacktrace();

}

return null;

}

public static JSONObject getAsJSONObjectFromObject(String json,String key) {

try {

JSONObject jsonObject = JSON.parSEObject(json);

return jsonObject.getJSONObject(key);

} catch (Exception e) {

e.printstacktrace();

}

return null;

}

public static JSONArray getAsJSONArrayFromObject(String json,String key) {

try {

JSONObject jsonObject = JSON.parSEObject(json);

return jsonObject.getJSONArray(key);

} catch (Exception e) {

e.printstacktrace();

}

return null;

}

public static String getAsstringFromObject(String json,String key) {

try {

JSONObject jsonObject = JSON.parSEObject(json);

return jsonObject.getString(key);

} catch (Exception e) {

e.printstacktrace();

}

return null;

}

public static Integer getAsIntegerFromObject(String json,String key) {

try {

JSONObject jsonObject = JSON.parSEObject(json);

return jsonObject.getInteger(key);

} catch (Exception e) {

e.printstacktrace();

}

return null;

}

public static Long getAsLongFromObject(String json,String key) {

try {

JSONObject jsonObject = JSON.parSEObject(json);

return jsonObject.getLong(key);

} catch (Exception e) {

e.printstacktrace();

}

return null;

}

public static Double getAsDoubleFromObject(String json,String key) {

try {

JSONObject jsonObject = JSON.parSEObject(json);

return jsonObject.getDouble(key);

} catch (Exception e) {

e.printstacktrace();

}

return null;

}

public static Boolean getAsBooleanFromObject(String json,String key) {

try {

JSONObject jsonObject = JSON.parSEObject(json);

return jsonObject.getBoolean(key);

} catch (Exception e) {

e.printstacktrace();

}

return null;

}

public static Short getAsShortFromObject(String json,String key) {

try {

JSONObject jsonObject = JSON.parSEObject(json);

return jsonObject.getShort(key);

} catch (Exception e) {

e.printstacktrace();

}

return null;

}

public static Byte getAsByteFromObject(String json,String key) {

try {

JSONObject jsonObject = JSON.parSEObject(json);

return jsonObject.getByte(key);

} catch (Exception e) {

e.printstacktrace();

}

return null;

}

public static <T> T getAsObjectFromObject(String json,String key,Class<T> clazz) {

try {

JSONObject jsonObject = JSON.parSEObject(json);

return jsonObject.getobject(key,clazz);

} catch (Exception e) {

e.printstacktrace();

}

return null;

}

public static <T> List<T> getAsListFromObject(String json,Class<T> clazz) {

try {

JSONObject jsonObject = JSON.parSEObject(json);

JSONArray jsonArray = jsonObject.getJSONArray(key);

return jsonArray.toJavaList(clazz);

} catch (Exception e) {

e.printstacktrace();

}

return null;

}

public static <T> T toJavaObject(JSONObject jsonObject,Class<T> clazz) {

try {

T t = jsonObject.toJavaObject(clazz);

return t;

} catch (Exception e) {

e.printstacktrace();

}

return null;

}

public static <T> List<T> toJavaList(JSONArray jsonArray,Class<T> clazz) {

try {

List<T> list = jsonArray.toJavaList(clazz);

return list;

} catch (Exception e) {

e.printstacktrace();

}

return null;

}

public static JSONObject parseJSONObject(Object object) {

try {

return JSON.parSEObject(toJson(object));

} catch (Exception e) {

e.printstacktrace();

}

return null;

}

public static JSONArray parseJSONArray(Object object) {

try {

return JSON.parseArray(toJson(object));

} catch (Exception e) {

e.printstacktrace();

}

return null;

}

//解决泛型问题 不能强制转换的方法

public static <T> T generateAssignObject(Object source,Class<T> clazz) {

try {

T t = toJavaObject(parseJSONObject(source),clazz);

return t;

} catch (Exception e) {

e.printstacktrace();

}

return null;

}

//解决泛型问题 不能强制转换的方法

public static <T> List<T> generateAssignList(Object source,Class<T> clazz) {

try {

List<T> list = toJavaList(parseJSONArray(source),clazz);

return list;

} catch (Exception e) {

e.printstacktrace();

}

return null;

}

}

相关文章

AJAX是一种基于JavaScript和XML的技术,能够使网页实现异步交...
在网页开发中,我们常常需要通过Ajax从后端获取数据并在页面...
在前端开发中,经常需要循环JSON对象数组进行数据操作。使用...
AJAX(Asynchronous JavaScript and XML)是一种用于创建 We...
AJAX技术被广泛应用于现代Web开发,它可以在无需重新加载页面...
Ajax是一种通过JavaScript和HTTP请求交互的技术,可以实现无...