JSONArray 循环遍历

Set<String> roleNames = new LinkedHashSet<String>();
        Set<String> permissions = new LinkedHashSet<String>();
        try {
            jsonStr = restTemplate.getForObject(url, String.class, urlVariables);
            if (StringUtils.isBlank(jsonStr)) {
                jsonStr = AUTH_CENTER_INTERFACE_ERROR;
            }
            JSONObject jsonObject = JSONObject.parseObject(jsonStr);

            if (null != jsonObject) {
                if (jsonObject.containsKey("errorCode") && jsonObject.getInteger("errorCode") == 0) {
                    if (jsonObject.containsKey("data")) {
                        JSONObject data = jsonObject.getJSONObject("data");
                        if (null != data && data.containsKey("roles")) {
                            JSONArray rolesArray = JSON.parseArray(data.getString("roles"));
                            for (int i = 0; i < rolesArray.size(); i++) {
                                JSONObject role = rolesArray.getJSONObject(i);
                                if (null != role && role.containsKey("name")) {
                                    String name = role.getString("name");
                                    if (StringUtils.isNotBlank(name)) {
                                        roleNames.add(name);
                                    }
                                }
                            }
                        }
                        if (permissionsLookupEnabled) {
                            if (null != data && data.containsKey("permissions")) {
                                JSONArray permissionsArray = JSON.parseArray(data.getString("permissions"));
                                for (int i = 0; i < permissionsArray.size(); i++) {
                                    JSONObject permission = permissionsArray.getJSONObject(i);
                                    if (null != permission && permission.containsKey("identifier")) {
                                        String identifier = permission.getString("identifier");
                                        if (StringUtils.isNotBlank(identifier)) {
                                            permissions.add(identifier);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
List<Picture> ret = new ArrayList<Picture>();
        String jsonStr = restTemplate.getForObject(phpUrl, String.class, vars);
        if (StringUtils.isNotBlank(jsonStr)) {
            JSONObject jsonObj = JSON.parseObject(jsonStr);
            int status = jsonObj.getIntValue("status");
            if (status == PHP_OP_SUCCESS) {
                ret = new ArrayList<Picture>();
                JSONObject dataJson = jsonObj.getJSONObject("content");
                JSONArray jsonList = dataJson.getJSONArray("data");
                for (Object obj : jsonList) {
                    JSONObject jobj = (JSONObject) obj;
                    int picId = jobj.getIntValue("pid");
                    String picUrl = jobj.getString("pUrl");
                    Picture pic = new Picture();
                    pic.setPicId(picId);
                    pic.setPicUrl(picUrl);
                    ret.add(pic);
                }
            } 
        }

相关推荐