Example usage for org.json JSONObject get

List of usage examples for org.json JSONObject get

Introduction

In this page you can find the example usage for org.json JSONObject get.

Prototype

public Object get(String key) throws JSONException 

Source Link

Document

Get the value object associated with a key.

Usage

From source file:account.management.controller.inventory.StockReportController.java

@Override
public void initialize(URL url, ResourceBundle rb) {
    product_list = FXCollections.observableArrayList();

    try {//from www. j  a va  2s  .c om
        HttpResponse<JsonNode> res = Unirest.get(MetaData.baseUrl + "all/products").asJson();
        JSONArray array = res.getBody().getArray();
        for (int i = 0; i < array.length(); i++) {
            JSONObject obj = array.getJSONObject(i);
            int id = obj.getInt("id");
            String name = obj.getString("name");
            float p_qty = Float.parseFloat(obj.get("p_qty").toString());
            float s_qty = Float.parseFloat(obj.get("s_qty").toString());
            double last_p_rate = obj.getDouble("last_p_rate");
            double last_s_rate = obj.getDouble("last_s_rate");
            double avg_p_rate = obj.getDouble("avg_p_rate");
            double avg_s_rate = obj.getDouble("avg_s_rate");

            product_list
                    .add(new Product(id, name, p_qty, s_qty, last_p_rate, last_s_rate, avg_p_rate, avg_s_rate));

        }
    } catch (Exception e) {
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setHeaderText(null);
        alert.setContentText("Sorry!! there is an error in the server. Please try again.");
        alert.setGraphic(new ImageView(new Image("resources/error.jpg")));
        alert.showAndWait();
    }

}

From source file:account.management.controller.inventory.StockReportController.java

@FXML
private void onShowClick(ActionEvent event) {
    this.show.setDisable(true);
    String start_date = "1980-01-01", end_date = "2050-12-31";
    try {/*  ww w . j  ava  2  s  .  c o  m*/
        start_date = new SimpleDateFormat("yyyy-MM-dd")
                .format(new SimpleDateFormat("yyyy-MM-dd").parse(this.start.getValue().toString()));
        end_date = new SimpleDateFormat("yyyy-MM-dd")
                .format(new SimpleDateFormat("yyyy-MM-dd").parse(this.end.getValue().toString()));

        HttpResponse<JsonNode> res = Unirest.get(MetaData.baseUrl + "report/stock")
                .queryString("start", start_date).queryString("end", end_date).asJson();
        JSONArray array = res.getBody().getArray();
        Vector v = new Vector();
        HashMap params = new HashMap();
        params.put("date",
                "From " + new SimpleDateFormat("dd-MM-yyyy")
                        .format(new SimpleDateFormat("yyyy-MM-dd").parse(start_date)) + " To "
                        + new SimpleDateFormat("dd-MM-yyyy")
                                .format(new SimpleDateFormat("yyyy-MM-dd").parse(end_date)));
        for (int i = 0; i < array.length(); i++) {
            JSONObject obj = array.getJSONObject(i);
            String id = String.valueOf(obj.getInt("item_id"));
            String name = getProductName(Integer.parseInt(id));
            String opening_qty = obj.get("opening_qty").toString();
            String opening_price = obj.get("opening_price").toString() == "null" ? "0.0"
                    : obj.get("opening_price").toString();
            String p_qty = obj.get("p_qty").toString() == "null" ? "0.0" : obj.get("p_qty").toString();
            String p_price = obj.get("p_price").toString() == "null" ? "0.0" : obj.get("p_price").toString();
            String s_qty = obj.get("s_qty").toString() == "null" ? "0.0" : obj.get("s_qty").toString();
            String s_price = obj.get("s_price").toString() == "null" ? "0.0" : obj.get("s_price").toString();

            String closing_qty = String
                    .valueOf(Float.parseFloat(opening_qty) + Float.parseFloat(p_qty) - Float.parseFloat(s_qty));

            String closing_total = String.valueOf(
                    Float.parseFloat(opening_price) + Float.parseFloat(p_price) - Float.parseFloat(s_price));
            String closing_rate = String
                    .valueOf(Float.parseFloat(closing_total) / Float.parseFloat(closing_qty));
            v.add(new Stock(0, id, name, opening_qty,
                    String.valueOf(Float.parseFloat(opening_price) / Float.parseFloat(opening_qty)),
                    opening_price, closing_qty, closing_rate, closing_total));

        }

        Report report = new Report();
        report.getReport("src\\report\\StockReport.jrxml", new JRBeanCollectionDataSource(v), params,
                "Stock Report");
        this.show.setDisable(false);
    } catch (Exception ex) {
        Logger.getLogger(StockReportController.class.getName()).log(Level.SEVERE, null, ex);
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setHeaderText(null);
        alert.setContentText("Sorry!! there is an error. Please try again.");
        alert.setGraphic(new ImageView(new Image("resources/error.jpg")));
        alert.showAndWait();
    }
}

From source file:account.management.controller.inventory.SellReportController.java

@FXML
private void onShowReportButtonClick(ActionEvent event) {
    this.show.setDisable(true);
    try {//  ww  w . j a v a 2  s . c  om

        String id = String.valueOf(this.item.getSelectionModel().getSelectedItem().getId());
        String start_date = "1980-01-01", end_date = "2050-12-31";

        start_date = new SimpleDateFormat("yyyy-MM-dd")
                .format(new SimpleDateFormat("yyyy-MM-dd").parse(this.start.getValue().toString()));
        end_date = new SimpleDateFormat("yyyy-MM-dd")
                .format(new SimpleDateFormat("yyyy-MM-dd").parse(this.end.getValue().toString()));

        HttpResponse<JsonNode> res = Unirest.get(MetaData.baseUrl + "report/ledger/sell").queryString("id", id)
                .queryString("start", start_date).queryString("end", end_date).asJson();
        JSONArray array = res.getBody().getArray();
        System.out.println(array);
        Vector v = new Vector();
        HashMap params = new HashMap();
        params.put("date",
                "From " + new SimpleDateFormat("dd-MM-yyyy")
                        .format(new SimpleDateFormat("yyyy-MM-dd").parse(start_date)) + " To "
                        + new SimpleDateFormat("dd-MM-yyyy")
                                .format(new SimpleDateFormat("yyyy-MM-dd").parse(end_date)));
        params.put("item_name",
                "Purchase report of " + this.item.getSelectionModel().getSelectedItem().getName());

        for (int i = 0; i < array.length(); i++) {
            JSONObject obj = array.getJSONObject(i);
            String date = obj.getString("date");
            String quantity = obj.get("quantity").toString();
            String rate = obj.get("rate").toString();
            v.add(new SellPurchaseLedger(date, quantity, rate));
        }
        Report report = new Report();
        report.getReport("src\\report\\SellPurchaseLedger.jrxml", new JRBeanCollectionDataSource(v), params,
                "Sell Report");
        report.getReport("src\\report\\DepositVoucher.jrxml", new JRBeanCollectionDataSource(v), params,
                "Deposit Voucher");
        this.show.setDisable(false);
    } catch (Exception e) {
        System.out.println("Exception in show report button click");
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setHeaderText(null);
        alert.setContentText("Sorry!! there is an error. Please try again.");
        alert.setGraphic(new ImageView(new Image("resources/error.jpg")));
        alert.showAndWait();
    }
}

From source file:account.management.controller.inventory.ProductWiseInventoryReportController.java

@FXML
private void onShowReportClick(ActionEvent event) {
    this.show.setDisable(true);
    try {/*  ww w .ja  v  a  2 s .c  om*/

        String id = String.valueOf(this.item.getSelectionModel().getSelectedItem().getId());
        String start_date = "1980-01-01", end_date = "2050-12-31";

        start_date = new SimpleDateFormat("yyyy-MM-dd")
                .format(new SimpleDateFormat("yyyy-MM-dd").parse(this.start.getValue().toString()));
        end_date = new SimpleDateFormat("yyyy-MM-dd")
                .format(new SimpleDateFormat("yyyy-MM-dd").parse(this.end.getValue().toString()));

        HttpResponse<JsonNode> res = Unirest.get(MetaData.baseUrl + "report/product/sellPurchase")
                .queryString("id", id).queryString("start", start_date).queryString("end", end_date).asJson();
        JSONArray array = res.getBody().getArray();
        Vector v = new Vector();
        HashMap params = new HashMap();
        params.put("date",
                "From " + new SimpleDateFormat("dd-MM-yyyy")
                        .format(new SimpleDateFormat("yyyy-MM-dd").parse(start_date)) + " To "
                        + new SimpleDateFormat("dd-MM-yyyy")
                                .format(new SimpleDateFormat("yyyy-MM-dd").parse(end_date)));
        params.put("item_name",
                "Inventory report of " + this.item.getSelectionModel().getSelectedItem().getName());
        float total_p_qty = 0, total_s_qty = 0;
        double total_p_param = 0;
        for (int i = 0; i < array.length(); i++) {
            JSONObject obj = array.getJSONObject(i);
            String date = obj.getString("date");
            date = new SimpleDateFormat("dd-MM-yyyy").format(new SimpleDateFormat("yyyy-MM-dd").parse(date));
            String p_qty = obj.get("p_qty").toString() == "null" ? "-" : obj.get("p_qty").toString();
            String p_rate = obj.get("p_rate").toString() == "null" ? "-" : obj.get("p_rate").toString();
            String p_price = obj.get("p_total").toString() == "null" ? "-" : obj.get("p_total").toString();
            String s_qty = obj.get("s_qty").toString() == "null" ? "-" : obj.get("s_qty").toString();
            String s_rate = obj.get("s_rate").toString() == "null" ? "-" : obj.get("s_rate").toString();
            String s_price = obj.get("s_total").toString() == "null" ? "-" : obj.get("s_total").toString();
            System.out.println(s_price);
            v.add(new IndividualProductReport(date, p_qty, p_rate, p_price, s_qty, s_rate, s_price));

            if (!p_qty.equals("-")) {
                total_p_qty += Float.parseFloat(p_qty);
            }
            if (!s_qty.equals("-")) {
                total_s_qty += Float.parseFloat(s_qty);
            }
            if (!p_price.equals("-")) {
                total_p_param += Float.parseFloat(p_price);
            }

        }
        params.put("closing_qty", String.valueOf(total_p_qty - total_s_qty));
        System.out.println(total_p_qty);
        params.put("closing_rate", String.valueOf(total_p_param / total_p_qty));
        params.put("closing_total",
                String.valueOf((total_p_qty - total_s_qty) * (total_p_param / total_p_qty)));
        Report report = new Report();
        report.getReport("src\\report\\IndiviualProductPurchaseSellReport.jrxml",
                new JRBeanCollectionDataSource(v), params, "Product Wise Inventory Report");
        this.show.setDisable(false);
    } catch (Exception e) {
        System.out.println("Exception in show report button click");
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setHeaderText(null);
        alert.setContentText("Sorry!! there is an error. Please try again.");
        alert.setGraphic(new ImageView(new Image("resources/error.jpg")));
        alert.showAndWait();
    }
}

From source file:account.management.controller.inventory.InsertStockController.java

/**
 * Initializes the controller class.//  ww w.j a va  2s  .co  m
 */
@Override
public void initialize(URL url, ResourceBundle rb) {

    // get product list
    products_list = FXCollections.observableArrayList();
    try {
        HttpResponse<JsonNode> res = Unirest.get(MetaData.baseUrl + "all/products").asJson();
        JSONArray array = res.getBody().getArray();
        for (int i = 0; i < array.length(); i++) {
            JSONObject obj = array.getJSONObject(i);
            int id = obj.getInt("id");
            String name = obj.getString("name");
            float p_qty = Float.parseFloat(obj.get("p_qty").toString());
            float s_qty = Float.parseFloat(obj.get("s_qty").toString());
            double last_p_rate = obj.getDouble("last_p_rate");
            double last_s_rate = obj.getDouble("last_s_rate");
            double avg_p_rate = obj.getDouble("avg_p_rate");
            double avg_s_rate = obj.getDouble("avg_s_rate");

            products_list
                    .add(new Product(id, name, p_qty, s_qty, last_p_rate, last_s_rate, avg_p_rate, avg_s_rate));

        }

        addRow();

    } catch (Exception e) {
    }

    // voucher type (purchase/sell)
    this.voucher_type.getItems().addAll("Purchase", "Sell");
    this.voucher_type.getSelectionModel().select("Sell");

}

From source file:account.management.controller.inventory.AllProductWiseReportController.java

@FXML
private void onShowReportClick(ActionEvent event) {
    this.show.setDisable(true);
    try {//w  w  w.  j  a  v a  2 s .c  o m
        String start_date = "1980-01-01", end_date = "2050-12-31";
        start_date = new SimpleDateFormat("yyyy-MM-dd")
                .format(new SimpleDateFormat("yyyy-MM-dd").parse(this.start.getValue().toString()));
        end_date = new SimpleDateFormat("yyyy-MM-dd")
                .format(new SimpleDateFormat("yyyy-MM-dd").parse(this.end.getValue().toString()));
        HttpResponse<JsonNode> res = Unirest.get(MetaData.baseUrl + "report/product/all/sellPurchase")
                .queryString("start", start_date).queryString("end", end_date).asJson();
        JSONArray array = res.getBody().getArray();
        Vector v = new Vector();
        HashMap params = new HashMap();
        params.put("date",
                "From " + new SimpleDateFormat("dd-MM-yyyy")
                        .format(new SimpleDateFormat("yyyy-MM-dd").parse(start_date)) + " To "
                        + new SimpleDateFormat("dd-MM-yyyy")
                                .format(new SimpleDateFormat("yyyy-MM-dd").parse(end_date)));
        for (int i = 0; i < array.length(); i++) {
            JSONObject obj = array.getJSONObject(i);
            String id = obj.get("id").toString().equals("null") ? "-" : obj.get("id").toString();
            String name = obj.get("name").toString().equals("null") ? "-" : obj.get("name").toString();
            String ob = obj.get("ob").toString().equals("null") ? "-" : obj.get("ob").toString();
            String p_qty = obj.get("p_qty").toString().equals("null") ? "-" : obj.get("p_qty").toString();
            String p_rate = obj.get("p_rate").toString().equals("null") ? "-" : obj.get("p_rate").toString();
            String p_total = obj.get("p_total").toString().equals("null") ? "-" : obj.get("p_total").toString();
            String s_qty = obj.get("s_qty").toString().equals("null") ? "-" : obj.get("s_qty").toString();
            String s_rate = obj.get("s_rate").toString().equals("null") ? "-" : obj.get("s_rate").toString();
            String s_total = obj.get("s_total").toString().equals("null") ? "-" : obj.get("s_total").toString();
            v.add(new AllProductWiseReport(id, name, ob, p_qty, p_rate, p_total, s_qty, s_rate, s_total));

        }
        Report report = new Report();
        report.getReport("src\\report\\AllProductPurchaseSellReport.jrxml", new JRBeanCollectionDataSource(v),
                params, "Products Report");
    } catch (ParseException ex) {
        Logger.getLogger(AllProductWiseReportController.class.getName()).log(Level.SEVERE, null, ex);
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setHeaderText(null);
        alert.setContentText("Please Enter date correctly.");
        alert.setGraphic(new ImageView(new Image("resources/error.jpg")));
        alert.showAndWait();
    } catch (UnirestException ex) {
        Logger.getLogger(AllProductWiseReportController.class.getName()).log(Level.SEVERE, null, ex);
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setHeaderText(null);
        alert.setContentText("Sorry!! there is an error in the server. Please try again.");
        alert.setGraphic(new ImageView(new Image("resources/error.jpg")));
        alert.showAndWait();
    } finally {
        this.show.setDisable(false);
    }

}

From source file:account.management.controller.inventory.PurchaseReportController.java

@FXML
private void onShowReportButtonClick(ActionEvent event) {
    this.show.setDisable(true);
    try {/*w  ww  . ja v a  2 s  . c  o  m*/

        String id = String.valueOf(this.item.getSelectionModel().getSelectedItem().getId());
        String start_date = "1980-01-01", end_date = "2050-12-31";

        start_date = new SimpleDateFormat("yyyy-MM-dd")
                .format(new SimpleDateFormat("yyyy-MM-dd").parse(this.start.getValue().toString()));
        end_date = new SimpleDateFormat("yyyy-MM-dd")
                .format(new SimpleDateFormat("yyyy-MM-dd").parse(this.end.getValue().toString()));

        HttpResponse<JsonNode> res = Unirest.get(MetaData.baseUrl + "report/ledger/purchase")
                .queryString("id", id).queryString("start", start_date).queryString("end", end_date).asJson();
        JSONArray array = res.getBody().getArray();
        System.out.println(array);
        Vector v = new Vector();
        HashMap params = new HashMap();
        params.put("date",
                "From " + new SimpleDateFormat("dd-MM-yyyy")
                        .format(new SimpleDateFormat("yyyy-MM-dd").parse(start_date)) + " To "
                        + new SimpleDateFormat("dd-MM-yyyy")
                                .format(new SimpleDateFormat("yyyy-MM-dd").parse(end_date)));
        params.put("item_name",
                "Purchase report of " + this.item.getSelectionModel().getSelectedItem().getName());

        for (int i = 0; i < array.length(); i++) {
            JSONObject obj = array.getJSONObject(i);
            String date = obj.getString("date");
            String quantity = obj.get("quantity").toString();
            String rate = obj.get("rate").toString();
            v.add(new SellPurchaseLedger(date, quantity, rate));
        }
        Report report = new Report();
        report.getReport("src\\report\\SellPurchaseLedger.jrxml", new JRBeanCollectionDataSource(v), params,
                "Purchase Report");
        this.show.setDisable(false);
    } catch (Exception e) {
        System.out.println("Exception in show report button click");
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setHeaderText(null);
        alert.setContentText("Sorry!! there is an error. Please try again.");
        alert.setGraphic(new ImageView(new Image("resources/error.jpg")));
        alert.showAndWait();
    }
}

From source file:org.everit.osgi.webconsole.configuration.ConfigServlet.java

private Map<String, List<String>> extractRawAttributesFromJSON(final JSONObject json) {
    Map<String, List<String>> rawAttributes = new HashMap<>();
    for (Object rawKey : json.keySet()) {
        String key = (String) rawKey;
        JSONArray valueArr = (JSONArray) json.get(key);
        int stringCount = valueArr.length();
        List<String> values = new ArrayList<String>(stringCount);
        for (int i = 0; i < stringCount; ++i) {
            Object value = valueArr.get(i);
            if (value.equals(JSONObject.NULL)) {
                values.add(null);// ww w . j av a  2 s .  c o m
            } else {
                values.add(value.toString());
            }
        }
        rawAttributes.put(key, values);
    }
    return rawAttributes;
}

From source file:org.loklak.api.iot.NetmonPushServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    Query post = RemoteAccess.evaluate(request);
    String remoteHash = Integer.toHexString(Math.abs(post.getClientHost().hashCode()));

    // manage DoS
    if (post.isDoS_blackout()) {
        response.sendError(503, "your request frequency is too high");
        return;/*from   ww w  .  ja  v  a2  s  .  c o m*/
    }

    String url = post.get("url", "");
    if (url == null || url.length() == 0) {
        response.sendError(400, "your request does not contain an url to your data object");
        return;
    }
    String screen_name = post.get("screen_name", "");
    if (screen_name == null || screen_name.length() == 0) {
        response.sendError(400, "your request does not contain required screen_name parameter");
        return;
    }

    JSONArray nodesList = new JSONArray();
    byte[] xmlText;
    try {
        xmlText = ClientConnection.download(url);
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document document = builder.parse(new InputSource(new StringReader(new String(xmlText))));
        NodeList routerList = document.getElementsByTagName("router");
        for (int i = 0; i < routerList.getLength(); i++) {
            JSONObject node = convertDOMNodeToMap(routerList.item(i));
            if (node != null)
                nodesList.put(node);
        }
    } catch (Exception e) {
        Log.getLog().warn(e);
        response.sendError(400, "error reading json file from url");
        return;
    }

    JsonFieldConverter converter = new JsonFieldConverter(
            JsonFieldConverter.JsonConversionSchemaEnum.NETMON_NODE);
    JSONArray nodes = converter.convert(nodesList);

    for (Object node_obj : nodes) {
        JSONObject node = (JSONObject) node_obj;
        if (!node.has("text")) {
            node.put("text", "");
        }
        node.put("source_type", SourceType.NETMON.toString());
        if (!node.has("user")) {
            node.put("user", new JSONObject());
        }

        List<Object> location_point = new ArrayList<>();
        location_point.add(0, Double.parseDouble((String) node.get("latitude")));
        location_point.add(1, Double.parseDouble((String) node.get("longitude")));
        node.put("location_point", location_point);
        node.put("location_mark", location_point);
        node.put("location_source", LocationSource.USER.name());
        try {
            node.put("id_str", PushServletHelper.computeMessageId(node, SourceType.NETMON));
        } catch (Exception e) {
            DAO.log("Problem computing id" + e.getMessage());
            continue;
        }
        try {
            JSONObject user = (JSONObject) node.get("user");
            user.put("screen_name", computeUserId(user.get("update_date"), user.get("id"), SourceType.NETMON));
        } catch (Exception e) {
            DAO.log("Problem computing user id : " + e.getMessage());
        }
    }

    PushReport pushReport = PushServletHelper.saveMessagesAndImportProfile(nodes, Arrays.hashCode(xmlText),
            post, SourceType.NETMON, screen_name);

    String res = PushServletHelper.buildJSONResponse(post.get("callback", ""), pushReport);
    response.getOutputStream().println(res);
    DAO.log(request.getServletPath() + " -> records = " + pushReport.getRecordCount() + ", new = "
            + pushReport.getNewCount() + ", known = " + pushReport.getKnownCount() + ", from host hash "
            + remoteHash);

}

From source file:com.microsoft.services.sharepoint.DocLibClient.java

/**
 * Retrieves the value of property with a given path and library
 * /*from   w w  w .j  a  v a  2 s.c o  m*/
 * @param property
 * @param path
 * @param library
 * @return
 */
public ListenableFuture<Object> getProperty(final String property, String path, String library) {
    if (path == null || path.length() == 0) {
        throw new IllegalArgumentException("Path cannot be null or empty");
    }

    if (property == null || property.length() == 0) {
        throw new IllegalArgumentException("Property cannot be null or empty");
    }

    String getPropertyUrl;
    if (library == null) {
        getPropertyUrl = getSiteUrl() + String.format("_api/files('%s')/%s", urlEncode(path), property);
    } else {
        String url = getSiteUrl() + "_api/web/Lists/GetByTitle('%s')/files('%s')/%s";
        getPropertyUrl = String.format(url, urlEncode(library.trim()), urlEncode(path), property);
    }

    final SettableFuture<Object> result = SettableFuture.create();
    ListenableFuture<JSONObject> request = executeRequestJson(getPropertyUrl, "GET");

    Futures.addCallback(request, new FutureCallback<JSONObject>() {
        @Override
        public void onFailure(Throwable t) {
            result.setException(t);
        }

        @Override
        public void onSuccess(JSONObject json) {
            Object propertyResult;
            try {
                propertyResult = json.getJSONObject("d").get(property);
                result.set(propertyResult);
            } catch (JSONException e) {
                result.setException(e);
            }
        }
    });
    return result;
}