遍歷json

JSON內容如下
{
“resultCode” : “success”,
“resultMsg” : “緩存查詢成功!”,
“cacheList” : [ {
“region” : “WebAppCacheRegion”,
“key” : “com.tianan.translate.data.service.impl.TianAnEnumSerivceImpl.getEnumName.insureService.passWord”,
“level” : “1”,
“size” : “432 bytes”
}, {
“region” : “WebAppCacheRegion”,
“key” : “com.tianan.translate.data.service.impl.TianAnEnumSerivceImpl.getEnumName.insureService.userName”,
“level” : “1”,
“size” : “416 bytes”
}, {
“region” : “WebAppCacheRegion”,
“key” : “com.tianan.translate.data.service.impl.TianAnEnumSerivceImpl.getEnumName.insureService.targetId”,
“level” : “1”,
“size” : “416 bytes”
} ]
}

獲取以上JSON中的cacheList

@RequestMapping(value = "/doGetCpfCacheJson.mvc")
    public @ResponseBody JSONObject doGetCpfCacheJson(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        JSONObject jsonObj = new JSONObject();
        List<Enum> ipList = tianAnEnumService.findByDictcode(Constants.CACHE_IP);

        try {
            JSONArray jsonList = new JSONArray();
            for (int i = 0; i < ipList.size(); i++) {
                JSONObject jsonObj2 = new JSONObject();
                String cacheStr = HttpUtils.postJsonBody("http://" + ipList.get(i).getCode() + ":8001/tianan_cpf/common/cache/cacheSelect.mvc", "");
                jsonObj2 = JSONObject.fromObject(cacheStr);
                JSONArray jsonList2 = JSONArray.fromObject(jsonObj2.get("cacheList"));
                if (jsonList2 != null) {
                    for (int j = 0; j < jsonList2.size(); j++) {
                        JSONObject subObj = (JSONObject) jsonList2.get(j);
                        subObj.put("region", subObj.get("region"));
                        subObj.put("key", subObj.get("key"));
                        subObj.put("level", subObj.get("level"));
                        subObj.put("size", subObj.get("size"));
                        jsonList.add(subObj);
                    }
                }
            }
            jsonObj.put("rows", jsonList);
            jsonObj.put("total", jsonList.size());
            logger.debug("緩存JSON數據:"+jsonObj.toString());
        } catch (Exception e) {
            logger.error("緩存請求過程中異常:"+e.getMessage(),e);
        }
        return jsonObj;
    }
發佈了70 篇原創文章 · 獲贊 20 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章