JSONObject、JSONArray結合Map的使用案例

1、Service中的實現方法:

JSONObject中有字符串,有JSONArray數組,而JSONArray數組中還有JSONObject。

/**  根據賬號查詢所持有的卡列表
 * @param weChatId
 * @return
 * {"Return":true,"list":[{"cardNo":"4047390001058975","cardtexts":"4047****8975","rule":{"cardDescribe":"","cardProductId":"","isGift":"","rules":"","state":0}},{"cardNo":"4564180001056860","cardtexts":"4564****6860","rule":{"cardDescribe":"","cardProductId":"","isGift":"","rules":"","state":0}},{"cardNo":"4564180001005206","cardtexts":"4564****5206","rule":{"cardDescribe":"","cardProductId":"","isGift":"","rules":"","state":0}},{"cardNo":"6221761700478669","cardtexts":"6221****8669","rule":{"cardDescribe":"","cardProductId":"","isGift":"","rules":"","state":0}},{"cardNo":"377186100023037","cardtexts":"3771****3037","rule":{"cardDescribe":"","cardProductId":"","isGift":"","rules":"","state":0}}]}
 */
public String queryCardNoList(String weChatId) {
	JSONObject result = new JSONObject();
	result.put("Return", true);//將標識字符串放入JSONObject中
	try {
		PwClientInfo pwClientInfo = CommonDao.getUserByWeChatId(weChatId);
		if (null != pwClientInfo) {
			CustomerCardRes res = CommonService.fetchLoopCustomerCardListNoCheckBlock(pwClientInfo, "", "");
			List list = res.getResList();
			CustomerCardResListObj obj = null;
			if (null != list && list.size() > 0) {
				JSONArray resultList = new JSONArray();
				for (int i = 0; i < list.size(); i++) {
					obj = (CustomerCardResListObj) list.get(i);
					PwGiftPointRule r = GiftPointDao.queryPointRule(weChatId, obj.getCardNo1());//DAO返回對象
					if (null == r || "1".equals(r.getIsGift())) {
						JSONObject j = new JSONObject();
						j.put("cardNo", obj.getCardNo1());//取對象的部分屬性放到JSONObject中
						j.put("cardtexts", obj.getCardNo1().substring(0, 4) + "****" + obj.getCardNo1().substring(obj.getCardNo1().length() - 4, obj.getCardNo1().length()));
						j.put("rule", null == r ? new PwGiftPointRule() : r);
						resultList.add(j);//將多個JSONObject放入JSONArray中
					} else {
						log.info(r.getCardProductId() + " " + obj.getCardNo1() + " " + r.getCardDescribe() + " 不參加活動!");
					}
				}
				result.put("list", resultList);//將JSONArray再放入JSONObject中
			}
		}
	} catch (Exception e) {
		result.put("Return", false);
		log.error("###獲取卡列表異常!", e);
	}
	return result.toString();//最後將JSONObject轉成字符串返回
}

2、部分servlet代碼:

String result = "";
JSONObject json = null;
result = queryCardNoList(weChatId);//調用service實現方法,獲得返回的JSONObject字符串

json = JSONObject.fromObject(result);//將JSONObject字符串轉換成JSONObject對象

if (json.getBoolean("Return")) {
	JSONArray jsa = json.getJSONArray("list");//獲取JSONObject中保存的JSONArray數組
	ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
	for (int i = 0; i < jsa.size(); i++) {//遍歷JSONArray數組
	    JSONObject cardObject = jsa.getJSONObject(i);//獲得JSONArray數組中的JSONObject對象
	    PwGiftPointRule rules=(PwGiftPointRule) JSONObject.toBean(cardObject.getJSONObject("rule"), PwGiftPointRule.class);
	    Map<String, String> m = new HashMap<String, String>();//將JSONObject轉換成實體類,然後將獲得的部分屬性放入字符串Map中
	    m.put("cardNo", cardObject.getString("cardNo"));
	    m.put("cardtexts", cardObject.getString("cardtexts"));
	    m.put("rule", "".equals(rules.getRules())?"1":rules.getRules());
	    m.put("cardProductId", rules.getCardProductId());
	    m.put("cardDescribe", "".equals(rules.getCardDescribe())?"卡名稱":rules.getCardDescribe());
	    list.add(m);//將Map放入ArrayList中
       }
       request.setAttribute("cardList", list);	//將ArrayList綁定到請求上並轉發到頁面				
       request.getRequestDispatcher("xxx.jsp").forward(request, response);
}

3、部分jsp代碼:


<select name="cardno" id="cardno"><--下拉框-->
	<option value="">
			請選擇卡
	</option>
	<%
		ArrayList al = (ArrayList) request.getAttribute("cardList");//獲得綁定的ArrayList
		if (al != null) {
			Map<String, String> m = null;
			String CardNoShow = "";
			for (int i = 0; i < al.size(); i++) {
				m = (Map<String, String>) al.get(i);//遍歷出ArrayList
	%>
	<option value="<%=m.get("cardNo")%>" rule="<%=m.get("rule")%>" cardName="<%=m.get("cardDescribe") %>" cardProductId="<%=m.get("cardProductId") %>"><--根據Map中的key獲得對象的屬性-->
            <%=m.get("cardtexts")%>
        </option>
        <%}}%>
</select>







發佈了40 篇原創文章 · 獲贊 148 · 訪問量 29萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章