import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;
import org.testng.annotations.Test;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
public class MyJSONUtil {
public static <T> T initSelfFromJSONObject(Class<T> clazz,
JSONObject parameterJSON) {
try {
Field[] fields = clazz.getDeclaredFields();
T t = clazz.newInstance();
for (Field f : fields) {
f.setAccessible(true);
f.set(t, getValueOfTypeTFromJSONObject(f.getType(), f.getName(),parameterJSON ));
}
return t;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private static <T> T getValueOfTypeTFromJSONObject(Class<T>clazz,String key,JSONObject jo) throws Exception{
if (clazz.equals(Integer.class)) {
return (T) jo.getInteger(key);
}else if(clazz.equals(Float.class)){
return (T) jo.getFloat(key);
}else if(clazz.equals(Double.class)){
return(T)jo.getDouble(key);
}else if(clazz.equals(String.class)) {
return (T)jo.getString(key);
}else if(clazz.equals(BigDecimal.class)){
return (T)jo.getBigDecimal(key);
}else if(clazz.equals(Boolean.class)){
return (T)jo.getBoolean(key);
}else if(clazz.equals(Date.class)){
return (T)jo.getDate(key);
}else if(clazz.equals(Long.class)){
return
递归比较两个JSONObject对象
最新推荐文章于 2022-11-18 10:22:26 发布