返回Json数据的方法

指定属性

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


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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值