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>







参与评论 您还未登录,请先 登录 后发表或查看评论

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:技术黑板 设计师:CSDN官方博客 返回首页

打赏作者

故渊池鱼

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值