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.fooock.shodan.model.host.FacetReportDeserializer.java

License:Open Source License

private List<Facet> getFacets(JsonObject facetsElement) {
    final List<Facet> facets = new ArrayList<>();

    for (Map.Entry<String, JsonElement> entry : facetsElement.entrySet()) {
        String key = entry.getKey();
        JsonElement property = entry.getValue();

        JsonArray jsonArray = property.getAsJsonArray();
        final List<Property> properties = new ArrayList<>(jsonArray.size());
        for (JsonElement element : jsonArray) {
            JsonObject facetElement = element.getAsJsonObject();
            JsonElement count = facetElement.get("count");
            JsonElement value = facetElement.get("value");

            final Property prop = new Property(count.getAsInt(), value.getAsString());
            properties.add(prop);//  w w  w .  j  a  va2s  .  c  om
        }
        final Facet facet = new Facet(key, properties);
        facets.add(facet);
    }
    return facets;
}

From source file:com.fred.controller.SiddhuCRUDController.java

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    if (request.getParameter("action") != null) {
        List<Rates> lstRate = new ArrayList<Rates>();
        String action = (String) request.getParameter("action");
        Gson gson = new Gson();
        response.setContentType("application/json");

        if (action.equals("list")) {
            try {
                lstRate = dao.getAllRates();
                JsonElement element = gson.toJsonTree(lstRate, new TypeToken<List<Rates>>() {
                }.getType());/*www . ja va2s.c o m*/
                JsonArray jsonArray = element.getAsJsonArray();
                String listData = jsonArray.toString();
                listData = "{\"Result\":\"OK\",\"Records\":" + listData + "}";
                response.getWriter().print(listData);
            } catch (Exception ex) {

                String error = "{\"Result\":\"ERROR\",\"Message\":" + ex.getMessage() + "}";
                response.getWriter().print(error);
                ex.printStackTrace();
            }
        } else if (action.equals("create") || action.equals("update")) {
            Rates rate = new Rates();

            if (request.getParameter("id") != null) {
                int id = Integer.parseInt(request.getParameter("id"));
                rate.setId(id);
            }

            if (request.getParameter("ratename") != null) {
                String ratename = (String) request.getParameter("ratename");
                rate.setRatename(ratename);
            }

            if (request.getParameter("rateValue") != null) {
                String ratevalue = (String) request.getParameter("ratevalue");
                rate.setRatevalue(ratevalue);
            }
            //if(request.getParameter(email?)!=null){
            //String email=(String)request.getParameter(email?);
            //user.setEmail(email);
            //}

            try {
                if (action.equals("create")) {
                    dao.addRate(rate);
                    lstRate.add(rate);
                    String json = gson.toJson(rate);
                    String listData = "{\"Result\":\"OK\",\"Record\":" + json + "}";
                    response.getWriter().print(listData);
                } else if (action.equals("update")) {
                    dao.updateRate(rate);
                    //String listData = ?{\?Result\?:\?OK\?}?;
                    String listData = "{\"Result\" : \"OK\"}";
                    response.getWriter().print(listData);
                }
            } catch (Exception ex) {
                String error = "{\"Result\":\"ERROR\",\"Message\":" + ex.getStackTrace().toString() + "}";
                response.getWriter().print(error);
                ex.printStackTrace();
            }
        } else if (action.equals("delete")) {//Delete record
            try {
                if (request.getParameter("id") != null) {
                    String id = (String) request.getParameter("id");
                    dao.deleteRate(Integer.parseInt(id));
                    String listData = "{\"Result\" : \"OK\"}";
                    response.getWriter().print(listData);
                }
            } catch (Exception ex) {
                String error = "{\"Result\":\"ERROR\",\"Message\":" + ex.getStackTrace().toString() + "}";
                response.getWriter().print(error);
                ex.printStackTrace();
            }
        }
    }
}

From source file:com.gemstore.servlet.Tables.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request/* w w  w. j a va 2 s .c  o m*/
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    // processRequest(request, response);

    String text = request.getParameter("choosen");
    System.out.println(text);
    JsonArray jsonArray = null;
    if (text.equalsIgnoreCase("customer")) {
        List<Customer> list = new ServiceUtil<Customer>().getEntitiesByPaging(Customer.class, 1);
        // List<Customer> list=new ServiceUtil<Customer>().getAllEntities(Customer.class);
        Gson gson = new Gson();
        JsonElement element = gson.toJsonTree(list, new TypeToken<List<Customer>>() {
        }.getType());
        jsonArray = element.getAsJsonArray();
    }
    response.setContentType("application/json");
    response.getWriter().print(jsonArray);
}

From source file:com.getperka.flatpack.codexes.ArrayCodex.java

License:Apache License

@Override
public T[] readNotNull(JsonElement element, DeserializationContext context) throws Exception {
    JsonArray array = element.getAsJsonArray();
    @SuppressWarnings("unchecked")
    T[] toReturn = (T[]) Array.newInstance(elementType, array.size());
    for (int i = 0, j = array.size(); i < j; i++) {
        context.pushPath("[" + i + "]");
        toReturn[i] = valueCodex.read(array.get(i), context);
        context.popPath();// w ww .  java2 s .co m
    }
    return toReturn;
}

From source file:com.getperka.flatpack.codexes.CollectionCodex.java

License:Apache License

@Override
public T readNotNull(JsonElement element, DeserializationContext context) throws Exception {
    T toReturn = newCollection();/*from   ww w  . j a  v a2  s .com*/
    JsonArray array = element.getAsJsonArray();
    int count = 0;
    for (JsonElement elt : array) {
        context.pushPath("[" + count++ + "]");
        toReturn.add(valueCodex.read(elt, context));
        context.popPath();
    }
    return toReturn;
}

From source file:com.github.autermann.matlab.json.MatlabValueSerializer.java

License:Open Source License

private MatlabMatrix parseMatlabMatrix(JsonElement value) {
    JsonArray array = value.getAsJsonArray();
    double[][] values = new double[array.size()][array.get(0).getAsJsonArray().size()];
    for (int i = 0; i < array.size(); i++) {
        JsonArray innerArray = array.get(i).getAsJsonArray();
        for (int j = 0; j < innerArray.size(); j++) {
            values[i][j] = innerArray.get(j).getAsDouble();
        }/*  w ww. j  av  a  2s .  c  o m*/
    }
    return new MatlabMatrix(values);
}

From source file:com.github.autermann.matlab.json.MatlabValueSerializer.java

License:Open Source License

private MatlabArray parseMatlabArray(JsonElement value) {
    JsonArray array = value.getAsJsonArray();
    double[] values = new double[array.size()];
    for (int i = 0; i < array.size(); i++) {
        values[i] = array.get(i).getAsDouble();
    }//w  w w. j  av  a2 s. c  o  m
    return new MatlabArray(values);
}

From source file:com.github.autermann.matlab.json.MatlabValueSerializer.java

License:Open Source License

private MatlabCell parseMatlabCell(JsonElement value) {
    JsonArray array = value.getAsJsonArray();
    MatlabValue[] cell = new MatlabValue[array.size()];
    for (int i = 0; i < array.size(); i++) {
        cell[i] = deserializeValue(array.get(i));
    }//from  w  ww .j a v  a2  s . c  om
    return new MatlabCell(cell);
}

From source file:com.github.frapontillo.categorize.wikipedia.rest.WikipediaResponseDeserializer.java

License:Apache License

@Override
public WikipediaResponse deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {

    WikipediaResponse response = new WikipediaResponse();

    // get the main query object
    JsonElement queryElement = json.getAsJsonObject().get("query");
    if (queryElement == null) {
        return response;
    }// w w w  .  j  a v a  2  s . co m
    // get the pages set
    Set<Map.Entry<String, JsonElement>> pages = queryElement.getAsJsonObject().get("pages").getAsJsonObject()
            .entrySet();

    // for each page, retrieve and add all categories
    for (Map.Entry<String, JsonElement> page : pages) {
        if (page.getKey().equals("-1")) {
            continue;
        }
        JsonElement categories = page.getValue().getAsJsonObject().get("categories");
        if (categories != null) {
            JsonArray jsonCategories = categories.getAsJsonArray();
            for (JsonElement jsonCategory : jsonCategories) {
                response.getCategories()
                        .add(new Category(jsonCategory.getAsJsonObject().get("title").getAsString()));
            }
        }
    }

    return response;
}

From source file:com.github.frapontillo.pulse.crowd.sentiment.sentit.rest.SentitResultMapDeserializer.java

License:Apache License

@Override
public SentitResponse.SentitResultMap deserialize(JsonElement json, Type typeOfT,
        JsonDeserializationContext context) throws JsonParseException {
    SentitResponse.SentitResultMap map = new SentitResponse.SentitResultMap();
    JsonArray array = json.getAsJsonArray();
    array.forEach(elem -> {//from   w ww . jav a 2 s  . c  om
        JsonObject obj = elem.getAsJsonObject();
        map.put(obj.get("id").getAsString(), new SentitResponse.SentitResult(
                obj.get("subjectivity").getAsString(), obj.get("polarity").getAsString()));
    });
    return map;
}