import net.sf.json.JSONObject; public class JsonTools {
public static JSONObject getJSONObject(String jData) {
if (jData == null || jData.length() <= 0)
return null;
JSONObject obj = JSONObject.fromObject(jData);
return obj;
} public static <T> String getJsonString(T t) {
JSONObject obj = JSONObject.fromObject(t);
return obj.toString();
} public static String getJsonValueByName(JSONObject object, String name) {
if (object == null)
return "--";
String value = object.getString(name);
if (null == value || "null".equalsIgnoreCase(value))
value = "";
return value;
} public static String getJsonValueByName(String jData, String name) {
if (jData == null || jData.length() <= 0)
return "";
JSONObject obj = JSONObject.fromObject(jData);
String value = obj.getString(name);
if (null == value || "null".equalsIgnoreCase(value))
value = "";
return value;
} public static String getJsonValueNotNull(String jData, String name)
throws Exception {
if (jData == null || jData.length() <= 0)
throw new Exception("获取请求数据[jsonData]为空");
JSONObject obj = JSONObject.fromObject(jData);
String value = obj.getString(name).trim();
if (null == value || value.length() <= 0)
throw new Exception("获取的数据对象[" + name + "]为空");
return value;
} @SuppressWarnings({ "rawtypes", "unchecked" })
public static <T> T getJavaBean(String jData, Class clazz) {
/*
* JSONObject jsonObject = JSONObject.fromObject(jData); return (T)
* JSONObject.toBean(jsonObject, clazz);
*/
JSONObject jsonObject = JSONObject.fromObject(jData);
return (T) JSONObject.toBean(jsonObject, clazz);
}
}