返回Json資料的方法

來源:互聯網
上載者:User

指定屬性

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();}}}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.