Example usage for com.google.gson JsonObject get

List of usage examples for com.google.gson JsonObject get

Introduction

In this page you can find the example usage for com.google.gson JsonObject get.

Prototype

public JsonElement get(String memberName) 

Source Link

Document

Returns the member with the specified name.

Usage

From source file:QuoteSetvlet.java

private void processPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    JsonObject newQuote = getJson(request);

    int newId = quotes.size() + 1;
    newQuote.addProperty("id", newId);

    String quote = newQuote.get("quote").getAsString();
    quotes.put(newId, quote);//from   w w  w . j  a va  2  s  .c o m

    out.println(newQuote);
}

From source file:QuoteSetvlet.java

private void processPut(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    int id = Integer.parseInt(request.getRequestURI().split("/")[3]);

    JsonObject newQuote = getJson(request);
    String quote = newQuote.get("quote").getAsString();

    quotes.put(id, quote);//from w w  w.  ja  v a2  s  .com

    newQuote.addProperty("id", id);
    out.println(newQuote);
}

From source file:RunnerRepository.java

License:Apache License

private static void populateLookAndFeels() {
    JsonObject looks = RunnerRepository.getLooks();
    if (looks != null) {
        int length = looks.entrySet().size();
        Iterator iter = looks.entrySet().iterator();
        Entry entry;//from   w ww .j a  va 2 s . c  o  m
        String[] vecresult;
        if (looks.get("DEFAULT") != null)
            lookAndFeels = new String[length - 1];
        else
            lookAndFeels = new String[length];
        int index = 0;
        for (int i = 0; i < length; i++) {
            entry = (Entry) iter.next();
            if (entry.getKey().toString().equals("DEFAULT"))
                continue;
            lookAndFeels[index] = (String) entry.getKey();
            index++;
        }
    } else {
        System.out.println("Error: No LooksAndFeels set, using default look");
    }
}

From source file:JsonConversion.java

public static void main(String[] args) {
    JSONObject obj = new JSONObject();
    obj.put("name", "mkyong.com");
    obj.put("age", new Integer(100));

    JSONArray list = new JSONArray();
    JSONObject obj1 = new JSONObject();
    obj1.put("1", "msg 1");
    list.add(obj1);// w w w  . ja  v  a  2 s. com

    JSONObject obj2 = new JSONObject();
    obj2.put("2", "msg 2");
    list.add(obj2);

    JSONObject obj3 = new JSONObject();
    obj3.put("3", "msg 3");
    list.add(obj3);

    obj.put("messages", list);
    String jsonStr1 = obj.toJSONString();
    String jsonStr = "{\"endLong\"=\"78.3769397\", \"endTime\"=\"16:48:28\", \"endLat\"=\"17.4466785\", \"tripData\"=\"[{Longitude:78.3769388,Latitude:17.4466736},{Longitude:78.3769388,Latitude:17.4466736}]\", \"endLoc\"=\"Deloitte Dr,Hyderabad,India500081\",\"emailId\"=\"user15@gmail.com\", \"tripId\"=\"30\"}";
    /*try {
            
    FileWriter file = new FileWriter("c:\\test.json");
    file.write(obj.toJSONString());
    file.flush();
    file.close();
            
    } catch (IOException e) {
    e.printStackTrace();
    }*/
    System.out.print(obj);
    JsonParser parser = new JsonParser();
    int firstIndex = jsonStr.indexOf("[");
    int lastIndex = jsonStr.indexOf("]");
    StringBuilder sb = new StringBuilder(jsonStr);
    sb.deleteCharAt(firstIndex - 1);
    sb.deleteCharAt(lastIndex);
    String str1 = sb.toString();
    Object objFinal = parser.parse(str1);
    JsonObject jsonObject = (JsonObject) objFinal;
    String name = String.valueOf(jsonObject.get("endLong"));
    System.out.println(name);

    Map msg = (Map) jsonObject.get("tripData");

}

From source file:JavaTestRunner.java

License:Apache License

public static Object runQuery(JavaQuery query, JsonObject combinedInput) {
    JsonArray hierarchy = combinedInput.getAsJsonArray("inheritance");
    JsonElement input = combinedInput.get("input");
    final ZonedDateTime now;
    JsonPrimitive primnow = combinedInput.getAsJsonPrimitive("now");
    if (primnow == null) {
        now = ZonedDateTime.now();
    } else {//w w w  .  j  a  v  a 2 s  . c  o m
        final String strnow = (String) (primnow.getAsString());
        now = ZonedDateTime.parse(strnow);
    }
    return runQuery(query, hierarchy, input, now);
}

From source file:Editors.java

License:Apache License

public String[] getEditors() {
    String[] vecresult;// w w w .j ava 2s . com
    JsonObject editors = RunnerRepository.getEditors();
    int length = editors.entrySet().size();
    if (editors.get("DEFAULT") != null)
        vecresult = new String[length - 1];
    else
        vecresult = new String[length];
    int index = 0;
    Entry entry;
    Iterator iter = editors.entrySet().iterator();
    for (int i = 0; i < length; i++) {
        entry = (Entry) iter.next();
        if (entry.getKey().toString().equals("DEFAULT"))
            continue;
        vecresult[index] = entry.getKey().toString();
        index++;
    }
    return vecresult;
}

From source file:PacketSnifferPlugin.java

License:Apache License

public void updateTable(String command) {
    HashMap<String, HashMap> hash = null;
    try {//from   w w w  .ja  v a  2s  . c o  m
        DefaultTableModel model = ((DefaultTableModel) jTable1.getModel());
        Object obj = client.execute("runPlugin",
                new Object[] { variables.get("user"), getName(), "command=query&data=" + index });
        //System.out.println("Response: "+obj.toString());

        hash = (HashMap<String, HashMap>) obj;
        //hash = (HashMap<String, HashMap>) client.execute("runPlugin",
        //      new Object[] { variables.get("user"), getName(),
        //            "command=query&data=" + index });
        HashMap hash2 = hash.get("data");
        if (hash2.isEmpty()) {
            return;
        }
        index = hash2.get("id").toString();
        //System.out.println("index: "+index);
        Object[] hash3 = (Object[]) hash2.get("packets");
        //System.out.println("packets length: "+hash3.length);

        String protocol, sip, smac, sport, dip, dmac, dport, id = null;
        for (Object ob : hash3) {
            try {
                JsonElement el = new JsonParser().parse(ob.toString());
                JsonObject jobject = el.getAsJsonObject();

                el = jobject.get("packet_head");
                jobject = el.getAsJsonObject();

                JsonElement source = jobject.get("source");
                jobject = source.getAsJsonObject();
                source = jobject.get("ip");
                sip = source.toString();
                sip = sip.substring(1, sip.length() - 1);
                source = jobject.get("mac");
                smac = source.toString();
                smac = smac.substring(1, smac.length() - 1);
                source = jobject.get("port");
                sport = source.toString();
                jobject = el.getAsJsonObject();
                source = jobject.get("destination");
                jobject = source.getAsJsonObject();
                source = jobject.get("ip");
                dip = source.toString();
                dip = dip.substring(1, dip.length() - 1);
                source = jobject.get("mac");
                dmac = source.toString();
                dmac = dmac.substring(1, dmac.length() - 1);
                source = jobject.get("port");
                dport = source.toString();
                jobject = el.getAsJsonObject();
                source = jobject.get("protocol");
                protocol = source.toString();
                protocol = protocol.substring(1, protocol.length() - 1);
                source = jobject.get("id");
                id = source.toString();
                id = id.substring(1, id.length() - 1);
                model.addRow(new Object[] { protocol, sip, smac, sport, dip, dmac, dport, id });
            } catch (Exception e) {
                System.out.println("Server response: " + hash.toString());
                e.printStackTrace();
            }
        }
        if (model.getRowCount() > length) {
            int delete = model.getRowCount() - length;
            for (int i = 0; i < delete; i++) {
                //System.out.println("delete");
                model.removeRow(0);
            }
        }
    } catch (Exception e) {
        System.out.println("Server response: " + hash.toString());
        e.printStackTrace();
    }

}

From source file:$.$.java

License:Apache License

  private String parseArg(String arguments, ResourceOperation operation, PropertyValue value,
    String object) {/*  w w w.  j a v a  2  s .c  om*/
  // parse the argument string and get the "value" parameter
  JsonObject args;
  String val = null;
  JsonElement jsonElem = null;
  Boolean passed = true;

  // check for parameters from the command
  if (arguments != null) {
    args = new JsonParser().parse(arguments).getAsJsonObject();
    jsonElem = args.get(object);
  }

  // if the parameter is passed from the command, use it, otherwise treat parameter
  // as the default
  if (jsonElem == null || jsonElem.toString().equals("null")) {
    val = operation.getParameter();
    passed = false;
  } else {
    val = jsonElem.toString().replace("\"", "");
  }

  // if no value is specified by argument or parameter, take the object default from the profile
  if (val == null) {
    val = value.getDefaultValue();
    passed = false;
  }

  // if a mapping translation has been specified in the profile, use it
  Map<String,String> mappings = operation.getMappings();
  if (mappings != null && mappings.containsKey(val)) {
    val = mappings.get(val);
    passed = false;
  }

  if (!value.mask().equals(BigInteger.ZERO) && passed) {
    val = transform.format(value, val);
  }

  return val;
}

From source file:account.PurchsaeReturnRegisterDetailAccount.java

private void jbtnViewActionPerformedRoutine() {
    try {//  w w w .j a  v  a  2 s.  co  m
        AccountAPI accountAPI = lb.getRetrofit().create(AccountAPI.class);

        if (ac_cd == null) {
            ac_cd = "";
        }
        if (!jCheckBox1.isSelected()) {
            ac_cd = "";
        }
        JsonObject call = accountAPI.PurchaseReturnRegisterAccount(jComboBox1.getSelectedIndex(),
                jComboBox3.getSelectedIndex(), lb.ConvertDateFormetForDB(jtxtFromDate.getText()),
                lb.ConvertDateFormetForDB(jtxtToDate.getText()), ac_cd).execute().body();

        lb.addGlassPane(this);
        if (call != null) {
            JsonObject result = call;
            if (result.get("result").getAsInt() == 1) {
                JsonArray array = call.getAsJsonArray("data");
                dtm.setRowCount(0);
                dtm.setRowCount(0);
                double cash = 0.00, bank = 0.00, card = 0.00;
                String tax_name = "";
                for (int i = 0; i < array.size(); i++) {
                    Vector row = new Vector();
                    row.add(array.get(i).getAsJsonObject().get("ref_no").getAsString());
                    row.add(array.get(i).getAsJsonObject().get("v_type").getAsString());
                    row.add(array.get(i).getAsJsonObject().get("inv_no").getAsString());
                    row.add(lb.ConvertDateFormetForDisplay(
                            array.get(i).getAsJsonObject().get("v_date").getAsString()));
                    row.add(array.get(i).getAsJsonObject().get("fname").getAsString());
                    row.add(array.get(i).getAsJsonObject().get("SR_ALIAS").getAsString());
                    cash = array.get(i).getAsJsonObject().get("CASH_AMT").getAsDouble();
                    bank = array.get(i).getAsJsonObject().get("BANK_AMT").getAsDouble();
                    card = array.get(i).getAsJsonObject().get("CARD_AMT").getAsDouble();
                    tax_name = array.get(i).getAsJsonObject().get("tax_name").getAsString();
                    row.add(array.get(i).getAsJsonObject().get("SR_NAME").getAsString());
                    row.add(array.get(i).getAsJsonObject().get("IMEI_NO").getAsString());
                    row.add(array.get(i).getAsJsonObject().get("SERAIL_NO").getAsString());
                    row.add(array.get(i).getAsJsonObject().get("QTY").getAsInt());
                    row.add(array.get(i).getAsJsonObject().get("RATE").getAsDouble());
                    row.add(array.get(i).getAsJsonObject().get("BASIC_ITEM_AMT").getAsDouble());
                    row.add(array.get(i).getAsJsonObject().get("tax_amt").getAsDouble());
                    row.add(array.get(i).getAsJsonObject().get("add_tax_amt").getAsDouble());
                    row.add(array.get(i).getAsJsonObject().get("DISC_RATE").getAsDouble());
                    row.add(array.get(i).getAsJsonObject().get("MRP").getAsDouble());
                    row.add(array.get(i).getAsJsonObject().get("AMT").getAsDouble());
                    row.add(array.get(i).getAsJsonObject().get("net_amt").getAsDouble());
                    row.add(tax_name);
                    row.add(cash);
                    row.add(bank);
                    row.add(card);
                    row.add(array.get(i).getAsJsonObject().get("bajaj_amt").getAsDouble());
                    row.add(lb.Convert2DecFmtForRs(
                            array.get(i).getAsJsonObject().get("net_amt").getAsDouble() - cash - bank - card
                                    - array.get(i).getAsJsonObject().get("bajaj_amt").getAsDouble()));
                    row.add(Constants.BRANCH.get(array.get(i).getAsJsonObject().get("branch_cd").getAsInt() - 1)
                            .getBranch_name());
                    dtm.addRow(row);
                }
                lb.setColumnSizeForTable(jTable1, jPanel1.getWidth());
                lb.removeGlassPane(PurchsaeReturnRegisterDetailAccount.this);
            } else {
                lb.removeGlassPane(PurchsaeReturnRegisterDetailAccount.this);
                lb.showMessageDailog(call.get("Cause").getAsString());
            }
        }
    } catch (IOException ex) {
        Logger.getLogger(PurchsaeReturnRegisterDetailAccount.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:account.SalesRegisterCardWise.java

private void jbtnViewActionPerformedRoutine() {
    try {/*from  ww  w. j  a v  a2  s.c om*/
        AccountAPI accountAPI = lb.getRetrofit().create(AccountAPI.class);

        JsonObject call = accountAPI.SalesRegisterCardWise(jCheckBox1.isSelected()).execute().body();

        lb.addGlassPane(this);

        if (call != null) {
            JsonObject result = call;
            if (result.get("result").getAsInt() == 1) {
                JsonArray array = call.getAsJsonArray("data");
                dtm.setRowCount(0);
                double net = 0.00;
                for (int i = 0; i < array.size(); i++) {
                    try {
                        Vector row = new Vector();
                        row.add(i + 1);
                        row.add((array.get(i).getAsJsonObject().get("AC_CD").getAsString()));
                        row.add((array.get(i).getAsJsonObject().get("FNAME") == null) ? ""
                                : array.get(i).getAsJsonObject().get("FNAME").getAsString());
                        row.add((array.get(i).getAsJsonObject().get("CARD_NO").getAsString()));
                        row.add((array.get(i).getAsJsonObject().get("MOBILE1").getAsString()));
                        row.add(lb.Convert2DecFmtForRs(
                                array.get(i).getAsJsonObject().get("NET_AMT").getAsDouble()));
                        dtm.addRow(row);
                        net += array.get(i).getAsJsonObject().get("NET_AMT").getAsDouble();
                    } catch (Exception e) {

                    }
                }

                Vector row = new Vector();
                row.add(" ");
                row.add(" ");
                row.add(" ");
                row.add(" ");
                row.add(" ");
                row.add(" ");
                dtm.addRow(row);

                row = new Vector();
                row.add("");
                row.add("");
                row.add("");
                row.add("");
                row.add("");
                row.add(lb.Convert2DecFmtForRs(net));
                dtm.addRow(row);

                lb.setColumnSizeForTable(jTable1, jPanel1.getWidth());
                lb.removeGlassPane(SalesRegisterCardWise.this);
            } else {
                lb.removeGlassPane(SalesRegisterCardWise.this);
                lb.showMessageDailog(call.get("Cause").getAsString());
            }
        }
    } catch (IOException ex) {
        Logger.getLogger(SalesRegisterCardWise.class.getName()).log(Level.SEVERE, null, ex);
    }
}