方法
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
public static <T> List<T> getListFromJson(Class<T> clzz, String params) throws RuleException {
if (StringUtils.isBlank(params)) {
throw new RuleException("-1000", "参数异常");
}
List<T> list = null;
try {
JSONArray entryArray = JSONObject.parseArray(params);
if ((null == entryArray) || (entryArray.isEmpty())) {
return list;
}
list = entryArray.toJavaList(clzz);
} catch (Exception e) {
throw new RuleException("-1000", "参数异常");
}
if (CollectionUtils.isEmpty(list)) {
throw new RuleException("-1000", "参数异常");
}
return list;
}
需要注意的点
如果json数据有double类型的,要转成java的Long等类型,方法调用前先处理一下,转成整数,否则上面的方法可能会报错