Example usage for com.google.gson JsonElement getAsJsonArray

List of usage examples for com.google.gson JsonElement getAsJsonArray

Introduction

In this page you can find the example usage for com.google.gson JsonElement getAsJsonArray.

Prototype

public JsonArray getAsJsonArray() 

Source Link

Document

convenience method to get this element as a JsonArray .

Usage

From source file:com.ajax.admin.AdminHomeAjax.java

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    try {//from w  ww. j  a  v  a2 s . co m
        if (conn == null || conn.isClosed()) {
            conn = (Connection) DBUtil.getConnection();
        }
        type = request.getParameter("type");
        branch_owner = request.getParameter("branch_owner");
        license_name = request.getParameter("license_name");
        type = request.getParameter("type");
        String cities = request.getParameter("city");
        String part_name = request.getParameter("part_name");
        String partner_id = request.getParameter("partner_id");
        String location = request.getParameter("location");
        String cityId = request.getParameter("city");
        String branch_id = request.getParameter("branch_id");
        String layout_id = request.getParameter("layout_id");
        String floor = request.getParameter("floor");
        String fac_type = request.getParameter("fac_type");

        if (type.equals("comp_name")) {
            partD = partnerRep.getAllPartners(cities, conn);
            Gson gson = new Gson();
            JsonElement element = gson.toJsonTree(partD, new TypeToken<List<PartnerDetails>>() {
            }.getType());
            JsonArray jsonArray = element.getAsJsonArray();
            response.setContentType("application/json");
            response.getWriter().print(jsonArray);
        } else if ("getLicense".equals(type)) {
            licenses = adminPartLicense.getLicenseNames(conn);
            gson = new Gson();
            element = gson.toJsonTree(licenses, new TypeToken<List<LicensePrice>>() {
            }.getType());
            jsonArray = element.getAsJsonArray();
            response.setContentType("application/json");
            response.getWriter().print(jsonArray);
        } else if ("manage_license".equals(type)) {
            licenseData = adminPartLicense.getLicenseData(branch_owner, conn);
            String jsonData = null;
            jsonArrayLicense = licenseData.get(0);
            jsonData = jsonArrayLicense.toString();
            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(jsonData);
        } else if ("License_Type".equals(type)) {
            licenseType = adminPartLicense.getLicenseType(license_name, conn);
            gson = new Gson();
            element = gson.toJsonTree(licenseType, new TypeToken<List<LicensePrice>>() {
            }.getType());
            jsonArray = element.getAsJsonArray();
            response.setContentType("application/json");
            response.getWriter().print(jsonArray);
        } else if (type.equals("Partner_Update_Details")) {
            String part_id = request.getParameter("part_id");
            prtId = partnerRep.getAllPartnerDetails(part_id, conn);
            Gson gson = new Gson();
            JsonElement element = gson.toJsonTree(prtId, new TypeToken<List<PartnerDetails>>() {
            }.getType());
            JsonArray jsonArray = element.getAsJsonArray();
            response.setContentType("application/json");
            response.getWriter().print(jsonArray);
        }
        if (type.equals("Partner_Report")) {
            compname = partnerRep.getAllData(cities, part_name, conn);
            Gson gson = new Gson();
            JsonElement element = gson.toJsonTree(compname, new TypeToken<List<PartnerDetails>>() {
            }.getType());
            JsonArray jsonArray = element.getAsJsonArray();
            response.setContentType("application/json");
            response.getWriter().print(jsonArray);
        } else if (type.equals("Vendor_City")) {
            JSONObject AdmCityListbj = null;
            try {
                AdmCityListbj = partDao.getAdmCityList(partner_id, conn);
            } catch (Exception e) {
                e.printStackTrace();
            }
            jsonData = AdmCityListbj.toString();
            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(jsonData);
        } else if (type.equals("Location")) {
            JSONObject AdmlocationList = null;
            try {
                AdmlocationList = AdmBranchlist.getAdmBranchLocation(cityId, partner_id, conn);
            } catch (Exception e) {
                e.printStackTrace();
            }
            jsonData = AdmlocationList.toString();
            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(jsonData);
        } else if (type.equals("Branch")) {
            JSONObject AdmBranchList = null;
            try {
                AdmBranchList = AdmBranchlist.getAdmBranchList(cityId, partner_id, location, conn);
            } catch (Exception e) {
                e.printStackTrace();
            }
            jsonData = AdmBranchList.toString();
            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(jsonData);
        } else if (type.equals("Branch1")) {
            JSONObject partBranchList = null;
            try {
                partBranchList = AdmBranchlist.getpartBranchList(cityId, partner_id, conn);
            } catch (Exception e) {
                e.printStackTrace();
            }
            jsonData = partBranchList.toString();
            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(jsonData);
        } else if (type.equals("Branch_Reports")) {
            JSONObject AdmBranchDatabj = null;
            try {
                AdmBranchDatabj = AdmBranchlist.getAdmBranchData(cityId, partner_id, location, branch_id, conn);
            } catch (Exception e) {
                e.printStackTrace();
            }
            jsonData = AdmBranchDatabj.toString();
            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(jsonData);
        } else if (type.equals("OnLoadlicense_name")) {
            JSONObject licen_name = null;
            try {
                licen_name = AdmBranchlist.getOnLoadLicenseName(branch_owner, conn);
            } catch (Exception e) {
                e.printStackTrace();
            }
            jsonData = licen_name.toString();
            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(jsonData);
        } else if (type.equals("Layout_Report")) {
            JSONObject Layout_Report = null;
            try {
                Layout_Report = AdmBranchlist.getAdmLayoutReport(cityId, location, branch_id, partner_id, conn);
            } catch (Exception e) {
                e.printStackTrace();
            }
            jsonData = Layout_Report.toString();
            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(jsonData);
        } else if (type.equals("floor_update")) {
            JSONObject floor_update = null;
            try {
                floor_update = AdmBranchlist.getAdmFloorDetails(layout_id, conn);
            } catch (Exception e) {
                e.printStackTrace();
            }
            jsonData = floor_update.toString();
            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(jsonData);
        } else if (type.equals("Floor")) {
            JSONObject addFloor = null;
            try {
                addFloor = AdminFacList.getAdmFloor(branch_id, conn);
            } catch (Exception e) {
                e.printStackTrace();
            }
            jsonData = addFloor.toString();
            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(jsonData);
        } else if (type.equals("fac_type")) {
            JSONObject factype = null;
            try {
                factype = AdminFacList.getfac_type(branch_id, floor, conn);
            } catch (Exception e) {
                e.printStackTrace();
            }
            jsonData = factype.toString();
            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(jsonData);
        } else if (type.equals("Facility_Reports")) {
            JSONObject Facility_Reports = null;
            try {
                Facility_Reports = AdminFacList.getFacilityReports(partner_id, cityId, location, branch_id,
                        floor, fac_type, conn);
            } catch (Exception e) {
                e.printStackTrace();
            }
            jsonData = Facility_Reports.toString();
            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(jsonData);
        } else if (type.equals("admin_license_name")) {
            JSONObject adm_license_name = null;
            try {
                adm_license_name = AdminFacList.onLoadLicenseName(partner_id, conn);
            } catch (Exception e) {
                e.printStackTrace();
            }
            jsonData = adm_license_name.toString();
            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(jsonData);
        } else if (type.equals("currency")) {
            JSONObject currency = null;
            try {
                currency = AdminFacList.getPartcurrency(partner_id, conn);
            } catch (Exception e) {
                e.printStackTrace();
            }
            jsonData = currency.toString();
            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(jsonData);
        } else if (type.equals("part_address")) {
            JSONObject part_address = null;
            try {
                part_address = AdminFacList.getPartcurrency(partner_id, conn);
            } catch (Exception e) {
                e.printStackTrace();
            }
            jsonData = part_address.toString();
            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(jsonData);
        }
        // Close connection
        if (conn != null || !conn.isClosed()) {
            DBUtil.closeConnection(conn);
            System.out.println(conn + " Connection closed");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.apcb.utils.utils.HtmlTemplateReader.java

private SectionMatch readJson(JsonElement jsonElement, String from, SectionMatch sectionMatch) {
    //log.info(jsonElement.toString());
    //SectionMatch sectionMatchChild = new SectionMatch(from);
    if (jsonElement.isJsonArray()) {
        //log.info("JsonArray");
        JsonArray jsonArray = jsonElement.getAsJsonArray();
        for (int i = 0; i < jsonArray.size(); i++) {
            SectionMatch sectionMatchArrayLevel = new SectionMatch(sectionMatch.getName());
            sectionMatchArrayLevel.setIndex(i);
            sectionMatch.putChildens(readJson(jsonArray.get(i), from + "[" + i + "]", sectionMatchArrayLevel));
        }/*from   ww  w.jav  a2s . com*/
    } else if (jsonElement.isJsonObject()) {
        //log.info("JsonObject");
        JsonObject jsonObject = jsonElement.getAsJsonObject(); //since you know it's a JsonObject
        Set<Map.Entry<String, JsonElement>> entries = jsonObject.entrySet();//will return members of your object
        for (Map.Entry<String, JsonElement> entry : entries) {
            //log.info(entry.getKey());
            sectionMatch.putChildens(readJson(jsonObject.get(entry.getKey()), from + "." + entry.getKey(),
                    new SectionMatch(entry.getKey())));
        }
    } else if (jsonElement.isJsonNull()) {
        log.info("JsonNull");
    } else if (jsonElement.isJsonPrimitive()) {
        //log.info("JsonPrimitive");
        String jsonVal = jsonElement.getAsString();
        //from+= "."+entry.getKey(); 
        sectionMatch.setValue(jsonVal);
        log.info(from + "=" + jsonVal);
    }

    return sectionMatch;
}

From source file:com.appdynamics.extensions.couchbase.CouchBaseWrapper.java

License:Apache License

/**
 * Gathers Bucket stats as Map of BucketName and Map of MetricName,
 * MetricValue/*from   w w w .  j  a v  a2 s. co  m*/
 * 
 * @param httpClient
 * @return Map<String, Map<String, Double>>
 */
public Map<String, Map<String, Double>> gatherBucketMetrics(SimpleHttpClient httpClient) {
    JsonElement bucketResponse = getResponse(httpClient, BUCKET_URI);
    Map<String, Map<String, Double>> bucketMetrics = new HashMap<String, Map<String, Double>>();
    if (bucketResponse != null && bucketResponse.isJsonArray()) {
        JsonArray bucketStats = bucketResponse.getAsJsonArray();
        bucketMetrics = populateBucketMetrics(bucketStats);
    }
    return bucketMetrics;
}

From source file:com.appspot.bitlyminous.gateway.DeliciousGateway.java

License:Apache License

/**
 * Gets the recent urls./*from  w  w w . ja  v  a  2  s.  c o m*/
 * 
 * @param tag the tag
 * 
 * @return the recent urls
 */
public List<Url> getRecentUrls(String tag) {
    GatewayApiUrlBuilder builder = createApiUrlBuilder(ApplicationConstants.DELICIOUS_RECENT_BY_TAG_URL);
    String apiUrl = builder.withField("tag", tag).buildUrl();
    JsonElement json = parseJson(callApiGet(apiUrl));
    return unmarshall(new TypeToken<List<Url>>() {
    }, json.getAsJsonArray());
}

From source file:com.appspot.bitlyminous.gateway.DeliciousGateway.java

License:Apache License

/**
 * Gets the popular urls./* w ww. j  a va 2  s.  c  o m*/
 * 
 * @param tag the tag
 * 
 * @return the popular urls
 */
public List<Url> getPopularUrls(String tag) {
    GatewayApiUrlBuilder builder = createApiUrlBuilder(ApplicationConstants.DELICIOUS_POPULAR_BY_TAG_URL);
    String apiUrl = builder.withField("tag", tag).buildUrl();
    JsonElement json = parseJson(callApiGet(apiUrl));
    return unmarshall(new TypeToken<List<Url>>() {
    }, json.getAsJsonArray());
}

From source file:com.arcbees.vcs.github.util.GitHubPullRequestsTypeAdapter.java

License:Apache License

@Override
public GitHubPullRequests deserialize(JsonElement json, Type typeOfT, final JsonDeserializationContext context)
        throws JsonParseException {
    JsonArray jsonArray = json.getAsJsonArray();

    List<GitHubPullRequest> pullRequests = FluentIterable.from(jsonArray)
            .transform(new Function<JsonElement, GitHubPullRequest>() {
                @Override//from  w  ww.  j  a  v a 2 s. c  om
                public GitHubPullRequest apply(JsonElement input) {
                    return context.deserialize(input, GitHubPullRequest.class);
                }
            }).toImmutableList();

    GitHubPullRequests gitHubPullRequests = new GitHubPullRequests();
    gitHubPullRequests.setPullRequests(pullRequests);

    return gitHubPullRequests;
}

From source file:com.atwelm.aezwidget.responses.openhab.OpenHABFetchLayoutResponse.java

License:Apache License

public static List<OpenHABWidget> parseWidgets(JsonElement widget, Gson gson) {
    List<OpenHABWidget> widgets = new LinkedList<OpenHABWidget>();

    if (widget != null) {
        if (widget.isJsonObject()) {
            OpenHABWidget innerWidget = gson.fromJson(widget, OpenHABWidget.class);
            widgets.add(innerWidget);/*ww  w  . j  av  a 2 s  .c o m*/
        } else if (widget.isJsonArray()) {
            JsonArray array = widget.getAsJsonArray();

            for (JsonElement element : array) {
                if (element.isJsonObject()) {
                    OpenHABWidget innerWidget = gson.fromJson(element, OpenHABWidget.class);
                    widgets.add(innerWidget);
                } else {
                    // Log error
                }
            }
        }
    }
    return widgets;
}

From source file:com.azure.webapi.JsonEntityParser.java

License:Open Source License

/**
 * Parses the JSON object to a typed list
 * //  w w w .j a  va 2s .c  om
 * @param results
 *            JSON results
 * @param gson
 *            Gson object used for parsing
 * @param clazz
 *            Target entity class
 * @return List of entities
 */
public static <E> List<E> parseResults(JsonElement results, Gson gson, Class<E> clazz) {
    List<E> result = new ArrayList<E>();
    String idPropertyName = getIdPropertyName(clazz);

    // Parse results
    if (results.isJsonArray()) // Query result
    {
        JsonArray elements = results.getAsJsonArray();

        for (JsonElement element : elements) {
            changeIdPropertyName(element.getAsJsonObject(), idPropertyName);
            E typedElement = gson.fromJson(element, clazz);
            result.add(typedElement);
        }
    } else { // Lookup result
        if (results.isJsonObject()) {
            changeIdPropertyName(results.getAsJsonObject(), idPropertyName);
        }
        E typedElement = gson.fromJson(results, clazz);
        result.add(typedElement);
    }
    return result;
}

From source file:com.baidu.cc.configuration.service.impl.VersionServiceImpl.java

License:Apache License

/**
 * ??./*from   w  w  w.ja v  a 2 s. com*/
 * 
 * @param file
 *            
 * @param versionId
 *            the version id
 * @throws IOException
 *             ?
 */
@Override
public void importFromFile(File file, Long versionId) throws IOException {
    byte[] byteArray = FileUtils.readFileToByteArray(file);

    Hex encoder = new Hex();
    try {
        byteArray = encoder.decode(byteArray);
    } catch (DecoderException e) {
        throw new IOException(e.getMessage());
    }
    String json = new String(byteArray, SysUtils.UTF_8);

    // parse from gson
    JsonParser jsonParser = new JsonParser();
    JsonElement je = jsonParser.parse(json);

    if (!je.isJsonArray()) {
        throw new RuntimeException("illegal json string. must be json array.");
    }

    JsonArray jsonArray = je.getAsJsonArray();

    int size = jsonArray.size();
    Version version = new Version();
    List<ConfigGroup> groups = new ArrayList<ConfigGroup>();
    ConfigGroup group;
    List<ConfigItem> items;
    ConfigItem item;

    for (int i = 0; i < size; i++) {
        JsonObject jo = jsonArray.get(i).getAsJsonObject();
        group = gson.fromJson(jo, ConfigGroup.class);

        // get sub configuration item
        JsonArray subItemsJson = jo.get(CONFIG_ITEMS_ELE).getAsJsonArray();

        int subSize = subItemsJson.size();
        items = new ArrayList<ConfigItem>();
        for (int j = 0; j < subSize; j++) {
            item = gson.fromJson(subItemsJson.get(j), ConfigItem.class);
            items.add(item);
        }

        group.setConfigItems(items);
        groups.add(group);
    }

    version.setConfigGroups(groups);
    configCopyService.copyConfigItemsFromVersion(version, versionId);
}

From source file:com.balajeetm.mystique.core.CopyMystTurn.java

License:Open Source License

@Override
public JsonElement transmute(List<JsonElement> source, JsonObject deps, JsonObject aces, JsonObject turn) {
    JsonElement transform = null;
    if (CollectionUtils.isNotEmpty(source)) {
        if (source.size() > 1) {
            transform = new JsonArray();
            for (JsonElement jsonElement : source) {
                JsonElement granularSource = getGranularSource(jsonElement, turn, deps, aces);
                granularSource = getSubset(granularSource, deps, aces, turn);
                transform.getAsJsonArray().add(granularSource);
            }//from   ww w .  j a  v a  2s.c om
        } else {
            JsonElement granularSource = getGranularSource(source.get(0), turn, deps, aces);
            transform = getSubset(granularSource, deps, aces, turn);
        }
    }
    return transform;
}