指定属性
JSONObject jsonObj = new JSONObject();
jsonObj.put("total", dispatchDetailList.size());
JsonConfig jsonConfig = new JsonConfig();
// 有级联,不能直接转化,要取出List放到map里面
// 过滤关联,避免死循环net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
jsonConfig.setJsonPropertyFilter(new PropertyFilter() {
public boolean apply(Object source, String name, Object value) {
if (name.equals("detailId") || name.equals("description") || name.equals("detailMoney")) {
//不进行过滤
return false;
} else {
return true;
}
}
});
jsonObj.elementOpt("rows", dispatchDetailList, jsonConfig);
try {
response.getWriter().println(jsonObj.toString());
} catch (IOException e) {
e.printStackTrace();
}
datagrid使用异步提交的方法
if ($('#applyDatagrid').datagrid('getSelected')) {
var row = $('#applyDatagrid').datagrid('getSelected');
$.messager.confirm('确认', '您确认想要提交记录吗?', function(r) {
if (r) {
$.ajax({
url : '${pageContext.request.contextPath}/travelAppAction!goTravelApp.action',
async : true,
type : 'POST',
dataType : 'JSON',
data : {
no : row.no
},
success : function(data) {
$.messager.alert('提示', data.msg);
$('#applyDatagrid').datagrid('reload');
}
});
}
});
} else {
$.messager.alert('提示', '请选择要提交的数据!');
}
使用maven导入json的jar包
<!-- 导入json所需jar包 -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
或者导入下面jar包
下拉列表
try {
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
String[] excludes = { "employees","travelApps" };
jsonConfig.setExcludes(excludes);
JSONArray jsonArr = JSONArray.fromObject(departmentList, jsonConfig);
response.getWriter().println(jsonArr.toString());
} catch (Exception e) {
e.printStackTrace();
}
try {
JSONObject jsonObj = new JSONObject();
jsonObj.put("total", empWorkPlanDetailList.size());
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor2());
jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
String[] excludes = { "empWorkPlanDetails","workStreams" };//排除的属性
jsonConfig.setExcludes(excludes);
jsonObj.elementOpt("rows", empWorkPlanDetailList, jsonConfig);
response.getWriter().println(jsonObj.toString());
} catch (Exception e) {
e.printStackTrace();
}
DateJsonValueProcessor2实体类
import java.text.SimpleDateFormat;
import java.util.Date;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
public class DateJsonValueProcessor2 implements JsonValueProcessor {
private String format = "yyyy-MM-dd HH:mm:ss";
private SimpleDateFormat sdf = new SimpleDateFormat(format);
public Object processArrayValue(Object value, JsonConfig arg1) {
// TODO Auto-generated method stub
return this.process(value);
}
public Object processObjectValue(String arg0, Object value, JsonConfig arg2) {
// TODO Auto-generated method stub
return this.process(value);
}
private Object process(Object value) {
if (value == null) {
return "";
} else if (value instanceof Date) {
return sdf.format((Date) value);
} else if (value instanceof Date[]) {
Date[] dates = (Date[]) value;
String[] obj = new String[dates.length];
for (int i = 0; i < dates.length; i++) {
obj[i] = sdf.format(dates[i]);
}
return obj;
} else {
return value.toString();
}
}
}