Java Code Examples for net.sf.json.JsonConfig#setExcludes()

The following examples show how to use net.sf.json.JsonConfig#setExcludes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GoodsAction.java    From xxshop with Apache License 2.0 6 votes vote down vote up
/**
	 * goodsIds用来接收来自前台的数据:包含了categoryId和num的json字符串
	 * result 则返回查询到的信息,也是封装成了一个json字符串
	 * @param goodsIds
	 * @param model
	 * @return
	 */
	@RequestMapping("/getGoodsesByIds")
	public String getGoodsesByIds(String goodsIds, Model model) {
//		System.out.println("goodsIds:" + goodsIds);
		String[] ids = goodsIds.split(",");
//		for (String s : ids) {
//			System.out.println("test:" + s);
//		}
//		System.out.println("test:" + ids.toString());
		List<Goods> goodses = goodsService.getGoodsByIds(ids);
		JsonConfig c = new JsonConfig();
		c.setExcludes(new String[] { "category", "goodsNo", "categoryId",
				"price1", "stock", "description", "role", "sellTime",
				"sellNum", "score" });
		JSONArray a = JSONArray.fromObject(goodses, c);
		String result = a.toString();
		model.addAttribute("result", result);
		System.out.println(result);
//		System.out.println("result:" + result);
		return "/TestJSP.jsp";
//		return "getgoodsesbyids";

	}
 
Example 2
Source File: BaseAction.java    From xmu-2016-MrCode with GNU General Public License v2.0 6 votes vote down vote up
public void writeJsonToResponse(Object o, Map<String, Object> otherParams) {
	JsonConfig config = new JsonConfig();
	config.setExcludes(new String[] { "addTime" });// 除去dept属�?
	HashMap<String, Object> tempMap = new HashMap<String, Object>();
	tempMap.put("Model", o);
	if (otherParams != null) {
		Iterator<Entry<String, Object>> iter = otherParams.entrySet()
				.iterator();
		while (iter.hasNext()) {
			Entry<String, Object> entry = iter.next();
			tempMap.put(entry.getKey(), entry.getValue());
		}
	}
	JSONObject json = JSONObject.fromObject(tempMap, config);
	writeStringToResponse(json.toString());
}
 
Example 3
Source File: BaseAction.java    From xmu-2016-MrCode with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 把Object转抱成Json时更改数据的默认转换格式
 * @param o 
 * @param otherParams 其它参数
 * @param excludes 不进行序列化属�?的名�?
 */
public void writeJsonToResponseWithDataFormat(Object o, Map<String, Object> otherParams, String excludes[]) {
	JsonConfig config = new JsonConfig();
	//更改Date类型转成json数据的格式化形式
	config.registerJsonValueProcessor(Date.class, new JsonValueFormat());
	config.setExcludes(excludes);// 除去dept属�?
	HashMap<String, Object> tempMap = new HashMap<String, Object>();
	tempMap.put("Model", o);
	if (otherParams != null) {
		Iterator<Entry<String, Object>> iter = otherParams.entrySet()
				.iterator();
		while (iter.hasNext()) {
			Entry<String, Object> entry = iter.next();
			tempMap.put(entry.getKey(), entry.getValue());
		}
	}
	JSONObject json = JSONObject.fromObject(tempMap, config);
	writeStringToResponse(json.toString().replaceAll("null", "[]"));
	
}
 
Example 4
Source File: MySurveyAction.java    From DWSurvey with GNU Affero General Public License v3.0 5 votes vote down vote up
public String attrs() throws Exception {
	HttpServletRequest request=Struts2Utils.getRequest();
	HttpServletResponse response=Struts2Utils.getResponse();
	try{
		SurveyDirectory survey=surveyDirectoryManager.getSurvey(id);
		JsonConfig cfg = new JsonConfig();
		cfg.setExcludes(new String[]{"handler","hibernateLazyInitializer"});
		JSONObject jsonObject=JSONObject.fromObject(survey,cfg);
		response.getWriter().write(jsonObject.toString());
	}catch(Exception e){
		e.printStackTrace();
	}
	return null;
}
 
Example 5
Source File: MessageServlet.java    From mytwitter with Apache License 2.0 5 votes vote down vote up
private void toAddFriend(HttpServletRequest request, HttpServletResponse response) throws IOException {
	HttpSession session = request.getSession();
	Users user = (Users) session.getAttribute("user");
	int fuid = user.getUid();
	List<Usersall> usersalls = messageDao.addFriend(fuid);
	JsonConfig config = new JsonConfig();
	config.setExcludes(new String[] { "upwd" });
	JSONArray array = JSONArray.fromObject(usersalls, config);
	response.getWriter().print(array.toString());
}
 
Example 6
Source File: MessageServlet.java    From mytwitter with Apache License 2.0 5 votes vote down vote up
private void toShuaxinMsg(HttpServletRequest request, HttpServletResponse response) throws IOException {
	String suid = request.getParameter("uid");
	String mid = request.getParameter("mid");
	HttpSession session = request.getSession();
	Users user = (Users) session.getAttribute("user");
	int fuid = user.getUid();
	List<Messageall> list = messageDao.shuaXin(fuid, mid, suid);
	if (list != null) {
		messageDao.toRead(fuid, suid);
		JsonConfig config = new JsonConfig();
		config.setExcludes(new String[] { "mtime" });
		JSONArray jsonArray = JSONArray.fromObject(list, config);
		response.getWriter().print(jsonArray.toString());
	}
}
 
Example 7
Source File: MessageServlet.java    From mytwitter with Apache License 2.0 5 votes vote down vote up
private void toGetMsg(HttpServletRequest request, HttpServletResponse response) throws IOException {
	String suid = request.getParameter("uid");
	HttpSession session = request.getSession();
	Users user = (Users) session.getAttribute("user");
	int fuid = user.getUid();
	List<Messageall> list = messageDao.findByTwoId(suid, fuid);
	if (list != null) {
		messageDao.toRead(fuid, suid);
		JsonConfig config = new JsonConfig();
		config.setExcludes(new String[] { "mtime" });
		JSONArray jsonArray = JSONArray.fromObject(list, config);
		response.getWriter().print(jsonArray.toString());
	}
}
 
Example 8
Source File: JsonUtil.java    From JavaEE-SSH-Template with MIT License 5 votes vote down vote up
public static JSONObject jsonFilter(Object obj, String[] filterNames){
    JsonConfig jsonConfig = new JsonConfig();
    jsonConfig.setIgnoreDefaultExcludes(false);    
    jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);    //防止自包含
     
    if(filterNames != null){
        //这里是核心,过滤掉不想使用的属性
        jsonConfig .setExcludes(filterNames) ;
    }
    JSONObject jsonObj = JSONObject.fromObject(obj, jsonConfig);
    return jsonObj;
     
}
 
Example 9
Source File: JsonUtil.java    From JavaEE-SSH-Template with MIT License 5 votes vote down vote up
public static JSONArray jsonListFilter(List objList, String[] filterNames){
    JsonConfig jsonConfig = new JsonConfig();
    jsonConfig.setIgnoreDefaultExcludes(false);    
    jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);    //防止自包含
     
    if(filterNames != null){
        //这里是核心,过滤掉不想使用的属性
        jsonConfig .setExcludes(filterNames) ;
    }
    JSONArray jsonArray = JSONArray.fromObject(objList, jsonConfig);
    return jsonArray;
     
}
 
Example 10
Source File: BaseServiceImpl.java    From xmu-2016-MrCode with GNU General Public License v2.0 5 votes vote down vote up
@Transactional
public String getJsonList(PageBean pageBean, Map<String, Object> map,
		String order, HttpServletRequest request,
		Map<String, Object> otherParams, String[] excludes) {
	if (order == null && request != null) {
		order = " ";
		order += StringUtils.isEmpty(request.getParameter("sortname")) ? " id"
				: " " + request.getParameter("sortname");
		order += StringUtils.isEmpty(request.getParameter("sortorder")) ? " desc"
				: " " + request.getParameter("sortorder");
	}
	List<T> list = new ArrayList<T>();
	list = getList(pageBean, map, order);
	HashMap<String, Object> tempMap = new HashMap<String, Object>();
	tempMap.put(Const.Page, pageBean);
	tempMap.put(Const.Rows, list);
	tempMap.put("Total", pageBean.getDataSize());
	if (otherParams != null) {
		Iterator<Entry<String, Object>> iter = otherParams.entrySet()
				.iterator();
		while (iter.hasNext()) {
			Entry<String, Object> entry = iter.next();
			tempMap.put(entry.getKey(), entry.getValue());
		}
	}
	JsonConfig config = new JsonConfig();
	if (excludes == null) {
		excludes = new String[] {}; // 待添�?
	}
	config.setExcludes(excludes);// 出去dept属�?
	JSONObject json = JSONObject.fromObject(tempMap, config);
	return json.toString();
}
 
Example 11
Source File: JsonUtil.java    From SmartEducation with Apache License 2.0 5 votes vote down vote up
public static JSONObject jsonFilter(Object obj, String[] filterNames){
    JsonConfig jsonConfig = new JsonConfig();
    jsonConfig.setIgnoreDefaultExcludes(false);    
    jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);    //防止自包含
    
    if(filterNames != null){
        //这里是核心,过滤掉不想使用的属性
        jsonConfig .setExcludes(filterNames) ;
    }
    JSONObject jsonObj = JSONObject.fromObject(obj, jsonConfig);
    return jsonObj;
}
 
Example 12
Source File: JsonUtil.java    From SmartEducation with Apache License 2.0 5 votes vote down vote up
public static JSONArray jsonListFilter(List objList, String[] filterNames){
    JsonConfig jsonConfig = new JsonConfig();
    jsonConfig.setIgnoreDefaultExcludes(false);    
    jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);    //防止自包含 
    //jsonConfig.registerJsonValueProcessor(java.sql.Timestamp.class, new DateJsonValueProcessor("yyyy-MM-dd HH:mm:ss")); 
    if(filterNames != null){
        //这里是核心,过滤掉不想使用的属性
        jsonConfig .setExcludes(filterNames) ;
    }
    JSONArray jsonArray = JSONArray.fromObject(objList, jsonConfig);
    return jsonArray;
}
 
Example 13
Source File: MessageServlet.java    From mytwitter with Apache License 2.0 4 votes vote down vote up
private void toGetLieBiao(HttpServletRequest request, HttpServletResponse response) throws IOException {
	HttpSession session = request.getSession();
	Users user = (Users) session.getAttribute("user");
	int uid = user.getUid();

	List<Messageall> listF = messageDao.findById(uid, 1);
	List<Messageall> listS = messageDao.findById(uid, 2);
	// List<Messageall> listT = new ArrayList<Messageall>();
	int num = 0;

	int fSize = listF.size();
	int sSize = listS.size();

	if (fSize != 0 && sSize == 0) {
		listS = listF;
	} else {
		if (fSize < sSize) {
			for (int i = 0; i < fSize; i++) {
				num = 0;
				for (int j = 0; j < sSize; j++) {
					if (listF.get(i).getSuid() != listS.get(j).getFuid()) {
						num++;
					}
					if (listF.get(i).getSuid() == listS.get(j).getFuid()) {
						if (listS.get(j).getMtime().before(listF.get(i).getMtime())) {
							listS.set(j, listF.get(i));
						}
					}
					if (num == sSize) {
						listS.add(listF.get(i));
					}
				}
			}
		} else {
			for (int i = 0; i < sSize; i++) {
				num = 0;
				for (int j = 0; j < fSize; j++) {
					if (listS.get(i).getFuid() != listF.get(j).getSuid()) {
						num++;
					}
					if (listS.get(i).getFuid() == listF.get(j).getSuid()) {
						if (listF.get(j).getMtime().before(listS.get(i).getMtime())) {
							listF.set(j, listS.get(i));
						}
					}
					if (num == fSize) {
						listF.add(listS.get(i));
					}
				}
			}
			listS = listF;
		}
	}
	Collections.sort(listS, (o1, o2) -> {
		return o2.getMtime().compareTo(o1.getMtime());
	});

	for (Messageall messageall : listS) {
		formatDate(messageall, messageall.getMtime());
	}
	JsonConfig config = new JsonConfig();
	config.setExcludes(new String[] { "mtime" });
	JSONArray jsonArray = JSONArray.fromObject(listS, config);
	response.getWriter().print(jsonArray.toString());
}
 
Example 14
Source File: BaseAction.java    From xmu-2016-MrCode with GNU General Public License v2.0 4 votes vote down vote up
public void writeJsonToResponse(List<?> list) {
	JsonConfig config = new JsonConfig();
	config.setExcludes(new String[] { "addTime"/*, "childAreas","suppliers"*/}); // 除去addTime属�?
	JSONArray json = JSONArray.fromObject(list, config); 
	writeStringToResponse(json.toString());
}
 
Example 15
Source File: OrderAction.java    From xmu-2016-MrCode with GNU General Public License v2.0 4 votes vote down vote up
public Mrcodeorder createOrder(){
	//生成码团订单
	//收集数据
	try {
		Customer customer = (Customer)session.get(Const.CUSTOMER);
		String roomIds = getParameter("room");
		String contactorIds = getParameter("contactor");
		Date begin = (Date)session.get("begin");
		Date end = (Date)session.get("end");
		List<Grouppurchasevoucher> vouchers = (List<Grouppurchasevoucher>)session.get("vouchers");
		Float depositPrice = (float) 0;
		for(Grouppurchasevoucher g : vouchers){
			depositPrice += g.getPrice();
		}
		//生成订单
		Mrcodeorder mrcodeorder = new Mrcodeorder(customer, 
				MakeOrderNum.makeOrderNum(Thread.currentThread().getName()), 
				depositPrice, new Timestamp(System.currentTimeMillis()), 
				new HashSet<Grouppurchasevoucher>(vouchers));
		mrcodeorder = mrcodeorderService.getById(mrcodeorderService.save(mrcodeorder));
		//顾客类型 1出差  2游玩
		String ids = passwordService.getLatestCity(customer, pageBean);
		customer.setCusType(Predict.trafficOrVisit(ids));
		//消费水平 1高 2低
		int shopLevel = passwordService.getShopLevel(customer, pageBean) > 300 ? 1 : 2 ;
		customer.setShopLevel(shopLevel );
		customerService.update(customer);
		
		//把团购券设为已使用
		for(Grouppurchasevoucher voucher : vouchers){
			voucher.setUsed(1);
		}
		grouppurchasevoucherService.saveOrUpdateAll(vouchers);
		//生成各房间的密码钥匙
		List<Password> passwords = null;
		if ((passwords=passwordService.createPasswords(mrcodeorder, roomIds, contactorIds, begin, end))!=null) {
			//请求酒店可用的房间
			String url_str = "http://localhost:8080/JavaPrj_9/reserv.htm?action=createReservBymrcode";//获取用户认证的帐号URL
	        URL url = new URL(url_str);
	        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			// connection.connect();
			// 默认是get 方式
			connection.setRequestMethod("POST");
			// 设置是否向connection输出,如果是post请求,参数要放在http正文内,因此需要设为true
			connection.setDoOutput(true);
			// Post 请求不能使用缓存
			connection.setUseCaches(false);
			//要上传的参数  
			JsonConfig config = new JsonConfig();
			config.setExcludes(new String[] { "roomtype", "floor","roomdates","mrcodeorder","customer"});
			config.registerJsonValueProcessor(Timestamp.class, new JsonValueFormat());
			JSONArray jsonArray = JSONArray.fromObject(passwords,config);
			JSONObject json = new JSONObject();
			json.put("deposit", 0);
			json.put("orderCode", mrcodeorder.getOrderCode());
			json.put("passwords", jsonArray);
	        PrintWriter pw=new PrintWriter(new OutputStreamWriter(connection.getOutputStream(),"utf-8"));
	        String content = "json=" + json;  
	        pw.print(content);
	        pw.flush();
	        pw.close();
	        int code = connection.getResponseCode();
	        if (code == 404) {
	            throw new Exception("连接无效,找不到此次连接的会话信息!");
	        }
	        if (code == 500) {
	            throw new Exception("连接服务器发生内部错误!");
	        }
	        if (code != 200) {
	            throw new Exception("发生其它错误,连接服务器返回 " + code);
	        }
	        InputStream is = connection.getInputStream();
	        byte[] response = new byte[is.available()];
	        is.read(response);
	        is.close();
	        if (response == null || response.length == 0) {
	            throw new Exception("连接无效,找不到此次连接的会话信息!");
	        }
	        mrcodeorder.setPasswords(new HashSet<Password>(passwords));
			return mrcodeorder;
		}else {
			return null;
		}
	} catch (Exception e) {
		// TODO: handle exception
		return null;
	}
	
}