JsonString 转 bean实体
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;
}
}


}
阅读更多
文章标签: java
个人分类: java
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

JsonString 转 bean实体

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭