JSONObject、JSONArray结合Map的使用案例

jopen 8年前

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转成字符串返回        }  
</div> </div>

2、部分servlet代码:

</div> </div>
    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>  
</div> </div> 来自:http://blog.csdn.net/zuoyixiao/article/details/41284917