天天看点

使用JSON传递接收集合,对象map for json,json for java

//发送方
public static void main(String[] args) {
		String mobiles = "15810022404";
		String content = "发送远程API测试短信。。。";
		boolean needReport = true;
		String xcode="01201112";//01为各平台短信子号,201112为流水号(000000-999999)
		String uname = "tudou";
		content = EncryptUtils.encodeBufferBase64(content.getBytes());// base64编码,防止乱码
		String sendtime = DateUtils.getNowTime();
		uname = EncryptUtils.encodeBufferBase64(uname.getBytes());
		Map<String, Object> map=new HashMap<String, Object>();//使用Map集合
		
		map.put("uname", uname);
		Send s=new Send();
		s.setContent(content);
		s.setStarttime(DateUtils.parseDate(sendtime));
		s.setMobile(mobiles);
		s.setNeedReport(needReport);
		s.setXcode(xcode);
		map.put("data", s);//将对象放入Map
		String md5 = MD5Utils.getInstance().getMD5ofStr(JSON.toJSONString(s)+MD5Utils.k1);
		map.put("md5", md5);
		new SmsSendTest().testSendSms(JSON.toJSONString(map));//直接将Map转换成JSON传递
	}
           

接收方:

public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		String json = request.getParameter("json").toString();
		JSONObject jo = JSON.parseObject(json);
		String md5 = jo.getString("md5");
		String data = jo.getString("data");
		System.out.println(data);
		Send send = JSON.parseObject(data, Send.class);
		String content = new String(EncryptUtils.decodeBufferBase64(send
				.getContent()));
		String uname = jo.getString("uname");
		String upwd = jo.getString("upwd");
		uname = new String(EncryptUtils.decodeBufferBase64(uname));
		boolean needReport = send.isNeedReport();
           

一个JSON封装数据的例子:

public String getModules(int todo, int offset, int pageSize, int total,
			int moduleNo) {
		total = getAllModules(todo, 0, 1, moduleNo).getTotal();
		int maxPage = total % pageSize == 0 ? total / pageSize : total/ pageSize + 1;
		if (offset < 0) {
			offset = 0;
		} else if (offset >= maxPage) {
			offset = maxPage - 1;
		}
		PageModel pm = getAllModules(todo, offset * pageSize, pageSize,moduleNo);
		List<Menu> list = pm.getDatas();
		JSONObject json = new JSONObject();
		try {
			JSONArray members = new JSONArray();
			if (list != null && list.size() > 0) {
				for (int i = 0; i < list.size(); i++) {
					JSONObject member = new JSONObject();
					member.put("menuId", list.get(i).getMenuId());
					member.put("menuNo", list.get(i).getMenuNo());
					member.put("parentNo", list.get(i).getParentNo());
					member.put("menuName", list.get(i).getMenuName());
					member.put("linkURL", list.get(i).getLinkURL());
					member.put("description", list.get(i).getDescription());
					members.add(i, member);
				}
				json.put("offset", offset);
				json.put("maxPage", maxPage - 1);
				json.put("total", total);
				json.put("menus", members);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return json.toString();
	}
           

在js中用ajax调用并显示:

function jsonview() {
	var total = $("total1").innerHTML;
	var offset = $("offset1").value;
	var pageSize = 5;
	menuDao.getModules(1,offset, pageSize, total, wirteHtml);
}
function wirteHtml(data) {
	var data = eval("(" + data + ")");// 使用eval函数
	var continents = data.menus;
	$("offset1").value = data.offset;
	$("maxPage1").value = data.maxPage;
	$("maxPage2").innerHTML = data.maxPage + 1;
	$("total1").innerHTML = data.total;
	$("curpage").innerHTML = data.offset + 1;
	var tab = $("mytables");
	var num = tab.rows.length;
	for ( var i = 0; i < num; i++) {
		if (tab.rows.length != 2) {
			tab.deleteRow(tab.rows.length - 2);
		}
	}
	for ( var i = 0; i < continents.length; i++) {
		var newLine = tab.rows.length;
		var row = tab.insertRow(newLine - 1);
		if (i % 2 != 0) {
			row.className = "odd";// 奇数行
			row.onmouseout = function() {
				this.className = "odd";
			}
		} else {
			row.className = "even";// 偶数行
			row.onmouseout = function() {
				this.className = "even";
			}
		}
		row.onmouseover = function() {
			this.className = "alt";
		}
		row.insertCell(0).innerHTML = "<input style=\"width:20px;\" type=\"radio\" name=\"rdoId\" value=\""
		+ continents[i].menuNo+ "\" title=\""+ continents[i].menuName+ "\" />";
		row.insertCell(1).innerHTML = continents[i].menuNo;
		row.insertCell(2).innerHTML = continents[i].menuName + " ";
		if(continents[i].description==''){
			row.insertCell(3).innerHTML = '暂无说明';
		}
		else{
			row.insertCell(3).innerHTML = continents[i].description;
		}
	}
}