指定屬性
JSONObject jsonObj = new JSONObject();jsonObj.put("total", dispatchDetailList.size());JsonConfig jsonConfig = new JsonConfig();// 有級聯,不能直接轉化,要取出List放到map裡面// 過濾關聯,避免死迴圈net.sf.json.JSONException: java.lang.reflect.InvocationTargetExceptionjsonConfig.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();}
datagrid格式(帶日期)
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 stubreturn this.process(value);}public Object processObjectValue(String arg0, Object value, JsonConfig arg2) {// TODO Auto-generated method stubreturn 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();}}}