创建JsonStringToClass对象即可
package utils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.text.SimpleDateFormat;
import java.util.*;
public class JsonStringToClass<T> {
public static Map<String,Object> ToMapOne(String obj){
Map<String,Object> map=null;
if(obj!=null&&obj.length()>0){
map = new HashMap<String,Object>();
JSONObject jsonOne = JSONObject.fromObject(obj);
Set<String> keys=jsonOne.keySet();
for(String key:keys){
map.put(key,jsonOne.get(key).toString());
}
}
return map;
}
public static Map<String,Object> ToMap(String obj){
Map<String,Object> map=null;
if(obj!=null&&obj.length()>0&&obj.indexOf("[")==0){
JSONArray json=JSONArray.fromObject(obj);
JSONObject jsonOne;
for(int i=0;i<json.size();i++){
map = new HashMap<String,Object>();
jsonOne = json.getJSONObject(i);
Set<String> keys=jsonOne.keySet();
for(String key:keys){
map.put(key,jsonOne.get(key).toString());
}
}
}
return map;
}
public static List<Map<String,Object>> StringToListMap(String obj){
List<Map<String,Object>> list=null;
if(obj!=null&&obj.length()>0&&obj.indexOf("[")==0) {
list = new ArrayList<>();
JSONArray json = JSONArray.fromObject(obj);
JSONObject jsonOne;
for (int i = 0; i < json.size(); i++) {
Map<String, Object> map = new HashMap<String, Object>();
jsonOne = json.getJSONObject(i);
Set<String> keys = jsonOne.keySet();
for (String key : keys) {
map.put(key, jsonOne.get(key));
}
list.add(map);
}
}
return list;
}
public static <T> T MapToClass(Map<String, Object> map, Class<T> clazz) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
if (map == null) {
return null;
}
T obj = null;
try {
obj = clazz.newInstance();
Field[] fields = obj.getClass().getDeclaredFields();
for (Field field : fields) {
int mod = field.getModifiers();
if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
continue;
}
field.setAccessible(true);
String filedTypeName = field.getType().getName();
String datetimestamp = map.get(field.getName())+"";
if (filedTypeName.equalsIgnoreCase("java.util.date")) {
if (datetimestamp.equalsIgnoreCase("null")) {
field.set(obj, null);
} else {
if(datetimestamp.length()<=10){
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
field.set(obj, sf.parse(datetimestamp));
}else{
field.set(obj, sdf.parse(datetimestamp));
}
}
continue;
}
if(filedTypeName.equalsIgnoreCase("java.lang.String")){
if (datetimestamp.equalsIgnoreCase("null")) {
field.set(obj, null);
} else {
field.set(obj, datetimestamp);
}
continue;
}
if(filedTypeName.equalsIgnoreCase("java.lang.Integer")){
if (datetimestamp.equalsIgnoreCase("null")) {
field.set(obj, null);
}else{
field.set(obj, new Integer(datetimestamp));
}
continue;
}
if(filedTypeName.equalsIgnoreCase("java.lang.Double")){
if (datetimestamp.equalsIgnoreCase("null")) {
field.set(obj, null);
}else {
field.set(obj, new Double(datetimestamp));
}
continue;
}
if(filedTypeName.equalsIgnoreCase("java.lang.Boolean")){
if (datetimestamp.equalsIgnoreCase("null")) {
field.set(obj, null);
}else {
field.set(obj, new Boolean(datetimestamp));
}
continue;
}
field.set(obj, map.get(field.getName()));
}
} catch (Exception e) {
e.printStackTrace();
}
return obj;
}
public List<T> ListMapToList(List<Map<String, Object>> listmap, Class<T> clazz){
List<T> list=new ArrayList<>();
for(Map<String,Object> map:listmap){
T bean=JsonStringToClass.MapToClass(map,clazz);
list.add(bean);
}
return list;
}
public <D> D StringJSONToList(String mql,Class<T> clazz){
D bean=null;
if(mql!=null&&mql.length()>0&&mql.indexOf("[")!=0){
Map<String,Object> map=JsonStringToClass.ToMapOne(mql);
if(map!=null&&map.size()>0){
bean=(D)JsonStringToClass.MapToClass(map,clazz);
}
}else if(mql!=null&&mql.length()>0&&mql.indexOf("[")==0){
List<Map<String,Object>> map=JsonStringToClass.StringToListMap(mql);
if(map!=null&&map.size()>0){
bean= (D) new JsonStringToClass().ListMapToList(map,clazz);
}
}
return bean;
}
}
实现方法
实体类对象 a=(实体类对象)new JsonStringToClass().StringJSONToList(json字符串,实体类对象.class);
实体类对象 a=(实体类对象)new JsonStringToClass().StringJSONToList(json字符串,实体类对象.class);