package com.ttd.utiliy;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import net.sf.json.JSONObject;
@SuppressWarnings("unchecked")
public class JsonStringToObject {
public static <T> T jsonToObject(Class<T> pojoCalss, HttpServletRequest request) {
Map<String, Object> map = JSONObject.fromObject(request.getAttribute("condition"));
String jsonString = JSONObject.fromObject(map).toString();
try {
Object pojo;
net.sf.json.JSONObject json = net.sf.json.JSONObject.fromObject(jsonString);
pojo = net.sf.json.JSONObject.toBean(json, pojoCalss);
return (T) pojo;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
public static <T> T jsonToObject(Class<T> pojoCalss, HashMap<String, Object> m) {
String jsonString = JSONObject.fromObject(m).toString();
try {
Object pojo;
net.sf.json.JSONObject json = net.sf.json.JSONObject.fromObject(jsonString);
pojo = net.sf.json.JSONObject.toBean(json, pojoCalss);
return (T) pojo;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
}
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import net.sf.json.JSONObject;
@SuppressWarnings("unchecked")
public class JsonStringToObject {
public static <T> T jsonToObject(Class<T> pojoCalss, HttpServletRequest request) {
Map<String, Object> map = JSONObject.fromObject(request.getAttribute("condition"));
String jsonString = JSONObject.fromObject(map).toString();
try {
Object pojo;
net.sf.json.JSONObject json = net.sf.json.JSONObject.fromObject(jsonString);
pojo = net.sf.json.JSONObject.toBean(json, pojoCalss);
return (T) pojo;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
public static <T> T jsonToObject(Class<T> pojoCalss, HashMap<String, Object> m) {
String jsonString = JSONObject.fromObject(m).toString();
try {
Object pojo;
net.sf.json.JSONObject json = net.sf.json.JSONObject.fromObject(jsonString);
pojo = net.sf.json.JSONObject.toBean(json, pojoCalss);
return (T) pojo;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
}