Example usage for org.json JSONArray length

List of usage examples for org.json JSONArray length

Introduction

In this page you can find the example usage for org.json JSONArray length.

Prototype

public int length() 

Source Link

Document

Get the number of elements in the JSONArray, included nulls.

Usage

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

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

    try {/*ww w .  ja va 2 s  . c o m*/
        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  a  va  2  s.  c om
        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

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

    new AutoCompleteComboBoxListener<>(item);
    item.setOnHiding((e) -> {
        Product a = item.getSelectionModel().getSelectedItem();
        item.setEditable(false);
        item.getSelectionModel().select(a);
    });
    item.setOnShowing((e) -> {
        item.setEditable(true);
    });

    // 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");
            products_list.add(new Product(id, name));

        }

        this.item.getItems().addAll(products_list);

    } 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.SellReportController.java

@FXML
private void onShowReportButtonClick(ActionEvent event) {
    this.show.setDisable(true);
    try {//ww  w. j a v a2  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/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 {//from  w w w .  j  a v a  2s .co 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/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:au.com.borner.salesforce.client.rest.domain.ContainerAsyncRequest.java

public List<CompilerError> getCompilerErrors() {
    // CompilerErrors is a JSON Array wrapped in a String ..... :-(
    String compilerErrors = getString("CompilerErrors");
    JSONArray jsonArray = new JSONArray(compilerErrors);
    List<CompilerError> result = new ArrayList<CompilerError>(jsonArray.length());
    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject jsonObject = jsonArray.getJSONObject(i);
        result.add(new CompilerError(jsonObject.toString()));
    }/*www  .ja v  a2s.c om*/
    return result;
}

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

/**
 * Initializes the controller class./*from  w  ww .ja  va 2  s .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 {//from   ww  w . j av  a2s . c om
        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:at.alladin.rmbt.db.QoSTestObjective.java

@SuppressWarnings("unchecked")
public String toHtml() {
    StringBuilder sb = new StringBuilder();
    sb.append("<h3>QoS-Test (uid: " + getUid() + ", test_class: " + getTestClass() + ")</h3>");
    if (getObjective() != null) {
        try {/*from  w  w w.j a  v  a 2  s.  c  o m*/
            JSONObject objectives = new JSONObject(getObjective());
            Iterator<String> keys = objectives.keys();
            sb.append("<b>Test objectives (as plain text):</b> <ul>");
            while (keys.hasNext()) {
                String key = keys.next();
                sb.append("<li><i>" + key + "</i>: " + objectives.optString(key) + "</li>");
            }
            sb.append("</ul>");
            sb.append("<b>Test objectives (as hstore representation):</b> "
                    + Helperfunctions.json2hstore(objectives, null) + "<br><br>");
            if (testSummary != null) {
                sb.append("<b>Test summary (test_summary):</b> <a href=\"#"
                        + testSummary.replaceAll("[\\-\\+\\.\\^:,]", "_") + "\">" + testSummary
                        + "</a><br><br>");
            } else {
                sb.append("<b>Test summary (test_summary):</b> <i>NULL</i><br><br>");
            }
            if (testDescription != null) {
                sb.append("<b>Test description (test_desc):</b> <a href=\"#"
                        + testDescription.replaceAll("[\\-\\+\\.\\^:,]", "_") + "\">" + testDescription
                        + "</a><br><br>");
            } else {
                sb.append("<b>Test description (test_desc):</b> <i>NULL</i><br><br>");
            }
        } catch (JSONException e) {
            sb.append(
                    "<b><i>incorrect test objectives format:</i></b><ul><li>" + getObjective() + "</li></ul>");
            e.printStackTrace();
        }
    } else {
        sb.append("<b><i>no objectives set for this test</i></b>");
    }

    sb.append("<b>Expected test results (as hstore representation):</b><ul>");
    if (getResults() != null) {
        JSONArray resultsJson;
        try {
            resultsJson = new JSONArray(getResults());
            for (int i = 0; i < resultsJson.length(); i++) {
                try {
                    final JSONObject expected = resultsJson.getJSONObject(i);
                    sb.append("<li>" + Helperfunctions.json2htmlWithLinks(expected) + "</li>");
                } catch (Exception e) {
                    e.printStackTrace();
                    sb.append("<li>incorrect expected test result format</li>");
                }
            }
        } catch (JSONException e1) {
            sb.append("<li>incorrect expected test result format</li>");
        }
    } else {
        sb.append("<li><i>No expected results set for this test</i></li>");
    }
    sb.append("</ul>");
    return sb.toString();
}

From source file:at.zone.madeleine.ir.PixlinqSearch.java

private String parsePixlinqResponse(String response) {
    JSONObject responseObject;/*  w w w . j a v  a 2  s  .  c o m*/
    String result = null;

    if (response != null) {
        try {
            responseObject = (JSONObject) new JSONTokener(response).nextValue();
            JSONArray matches = responseObject.getJSONArray("matches");
            JSONArray errors = responseObject.getJSONArray("errors");
            if (matches.length() > 0) {
                JSONObject match = matches.getJSONObject(0);
                String title = match.getString("title");
                String message = match.getString("message");
                String sanatizedMessage = message.split("\\.")[0];

                result = sanatizedMessage;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    } else {
        result = null;
    }
    return result;
}