Example usage for org.json JSONObject getDouble

List of usage examples for org.json JSONObject getDouble

Introduction

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

Prototype

public double getDouble(String key) throws JSONException 

Source Link

Document

Get the double value 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  ww w . j a va  2s  .  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.InsertStockController.java

/**
 * Initializes the controller class.// w  ww .  j  a v  a2 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:com.nextgis.maplib.display.SimpleLineStyle.java

@Override
public void fromJSON(JSONObject jsonObject) throws JSONException {
    super.fromJSON(jsonObject);
    mWidth = (float) jsonObject.getDouble(JSON_WIDTH_KEY);
}

From source file:org.achtern.AchternEngine.core.resource.loader.json.JsonLoader.java

/**
 * Reads a {@link org.achtern.AchternEngine.core.rendering.Color} from a JSON
 * declaration.//from   w  w w  . ja  va  2 s . co m
 * Components not specified will default to <code>1</code>.
 * @param json JSONObject
 * @return Color
 */
protected Color getColor(JSONObject json) {
    float r, g, b, a;
    // Default to 1
    r = g = b = a = 1;

    if (json.has("r")) {
        r = (float) json.getDouble("r");
    }
    if (json.has("g")) {
        g = (float) json.getDouble("g");
    }
    if (json.has("b")) {
        b = (float) json.getDouble("b");
    }
    if (json.has("a")) {
        a = (float) json.getDouble("a");
    }

    return new Color(r, g, b, a);
}

From source file:com.cssweb.android.view.KlineView.java

public void drawQuoteWin(Canvas canvas, JSONObject quoteData, int idx) throws JSONException {
    Paint mPaint = new Paint();
    mPaint.setTextAlign(Paint.Align.LEFT);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setTypeface(Typeface.DEFAULT_BOLD);
    mPaint.setAntiAlias(true);/*from w ww . j  a  va 2s . c om*/

    mPaint.setColor(GlobalColor.colorKlinePopub);
    mPaint.setTextSize(sTextSize);
    canvas.drawText(":", 0, axisLabelHeight, mPaint);
    canvas.drawText(":", 0, axisLabelHeight * 3, mPaint);
    canvas.drawText(":", 0, axisLabelHeight * 5, mPaint);
    canvas.drawText(":", 0, axisLabelHeight * 7, mPaint);
    canvas.drawText(":", 0, axisLabelHeight * 9, mPaint);
    canvas.drawText(":", 0, axisLabelHeight * 11, mPaint);
    canvas.drawText("??:", 0, axisLabelHeight * 13, mPaint);
    canvas.drawText("??:", 0, axisLabelHeight * 15, mPaint);

    mPaint.setTextAlign(Paint.Align.RIGHT);
    String qt = quoteData.getJSONArray("K").getJSONArray(idx).getString(0);
    double jrkp = quoteData.getJSONArray("K").getJSONArray(idx).getDouble(1);
    double zg = quoteData.getJSONArray("K").getJSONArray(idx).getDouble(2);
    double zd = quoteData.getJSONArray("K").getJSONArray(idx).getDouble(3);
    double sp = quoteData.getJSONArray("K").getJSONArray(idx).getDouble(4);
    double preclose;
    if ("cf".equals(exchange) || "dc".equals(exchange) || "sf".equals(exchange) || "cz".equals(exchange)) {
        switch (idx) {
        case 0:
            preclose = quoteData.getJSONArray("K").getJSONArray(idx).getDouble(7);
            break;
        default:
            preclose = quoteData.getJSONArray("K").getJSONArray(idx - 1).getDouble(7);
            break;
        }
        if (quoteData.getJSONArray("K").length() == 1) {
            preclose = quoteData.getJSONArray("K").getJSONArray(0).getDouble(7);
        }
    } else {
        switch (idx) {
        case 0:
            preclose = quoteData.getJSONArray("K").getJSONArray(idx).getDouble(4);
            break;
        default:
            preclose = quoteData.getJSONArray("K").getJSONArray(idx - 1).getDouble(4);
            break;
        }
        if (quoteData.getJSONArray("K").length() == 1) {
            preclose = quoteData.getDouble("zrsp");//quoteData.getJSONArray("K").getJSONArray(0).getDouble(4);
        }
    }
    mPaint.setColor(getcolor(jrkp, preclose));
    canvas.drawText(Utils.dataFormation(jrkp, stockdigit), klineX, axisLabelHeight * 2, mPaint);
    mPaint.setColor(getcolor(zg, preclose));
    canvas.drawText(Utils.dataFormation(zg, stockdigit), klineX, axisLabelHeight * 4, mPaint);
    mPaint.setColor(getcolor(zd, preclose));
    canvas.drawText(Utils.dataFormation(zd, stockdigit), klineX, axisLabelHeight * 6, mPaint);
    mPaint.setColor(getcolor(sp, preclose));
    canvas.drawText(Utils.dataFormation(sp, stockdigit), klineX, axisLabelHeight * 8, mPaint);
    double zhangdie = sp - preclose;
    if (zhangdie > 0)
        mPaint.setColor(GlobalColor.colorpriceUp);
    else if (zhangdie < 0)
        mPaint.setColor(GlobalColor.colorPriceDown);
    else
        mPaint.setColor(GlobalColor.colorPriceEqual);
    canvas.drawText(Utils.dataFormation(zhangdie, stockdigit), klineX, axisLabelHeight * 10, mPaint);
    //      if(quoteData.getString("period").equals("min5") || 
    //            quoteData.getString("period").equals("min15") ||
    //            quoteData.getString("period").equals("min30") ||
    //            quoteData.getString("period").equals("min60")){
    //         qt = qt.substring(4,6)+'/'+qt.substring(6,8)+' '+qt.substring(8);
    //      }else{
    //         qt = qt.substring(2,4)+qt.substring(4,6)+qt.substring(6,8);
    //      }
    qt = qt.substring(2, 4) + qt.substring(4, 6) + qt.substring(6, 8);
    mPaint.setColor(GlobalColor.colorLabelName);
    canvas.drawText(qt, klineX, axisLabelHeight * 12, mPaint);

    mPaint.setColor(GlobalColor.colorStockName);
    canvas.drawText(Utils.getAmountFormat(quoteData.getJSONArray("K").getJSONArray(idx).getDouble(5), false),
            klineX, axisLabelHeight * 14, mPaint);
    mPaint.setColor(GlobalColor.colorStockName);
    canvas.drawText(Utils.getAmountFormat(quoteData.getJSONArray("K").getJSONArray(idx).getDouble(6), false),
            klineX, axisLabelHeight * 16, mPaint);
}

From source file:com.fortydegree.ra.data.JsonUnmarshaller.java

public static Marker processGeoserviceJSONObject(JSONObject jo) throws JSONException {
    String type = jo.getString("type");
    String metadata = jo.getString("metadata");
    JSONObject jsonMetadata = new JSONObject(metadata);
    String title = jsonMetadata.optString("title");

    Marker m = new Marker(jo.getDouble("latitude"), jo.getDouble("longitude"), jo.getDouble("altitude"));
    m.title = title;/* w  w  w  .j a v  a 2  s  . co m*/
    m.distance = jo.getDouble("distance");

    @SuppressWarnings("rawtypes")
    Iterator metadataIter = jsonMetadata.keys();
    while (metadataIter.hasNext()) {
        String key = metadataIter.next().toString();
        m.setData(key, jsonMetadata.getString(key));
    }
    m.setData("title", title);
    m.setData("type", type);

    return m;
}

From source file:gmc.hotplate.util.JsonParser.java

public Ingredient parseIngredientObject(JSONObject obj) {
    Ingredient ingredient = null;//  w  ww . j a v  a 2  s .c  o m
    try {
        String name = obj.getString(TAG_INGREDIENTS_NAME);
        double amount = obj.getDouble(TAG_INGREDIENTS_AMOUNT);
        String type = obj.getString(TAG_INGREDIENTS_TYPE);
        ingredient = new Ingredient(name, amount, type);
    } catch (JSONException e) {
        Log.w(LOG_TAG, "ParseIngredient() error: " + e.getMessage());
    }
    return ingredient;
}

From source file:ti.mobileapptracker.MobileapptrackerModule.java

private List<MATEventItem> convertToMATEventItems(Object[] arrItemMaps) {
    List<MATEventItem> listItems = new ArrayList<MATEventItem>();

    try {//from www . j ava 2s  .c  o m
        JSONArray arr = new JSONArray(Arrays.toString(arrItemMaps));

        for (int i = 0; i < arr.length(); i++) {
            JSONObject item = arr.getJSONObject(i);

            String itemName = item.getString("item");
            int quantity = 0;
            double unitPrice = 0;
            double revenue = 0;
            String attribute1 = null;
            String attribute2 = null;
            String attribute3 = null;
            String attribute4 = null;
            String attribute5 = null;

            if (item.has("quantity")) {
                quantity = item.getInt("quantity");
            }
            if (item.has("unit_price")) {
                unitPrice = item.getDouble("unit_price");
            }
            if (item.has("revenue")) {
                revenue = item.getDouble("revenue");
            }

            if (item.has("attribute_sub1")) {
                attribute1 = item.getString("attribute_sub1");
            }
            if (item.has("attribute_sub2")) {
                attribute2 = item.getString("attribute_sub2");
            }
            if (item.has("attribute_sub3")) {
                attribute3 = item.getString("attribute_sub3");
            }
            if (item.has("attribute_sub4")) {
                attribute4 = item.getString("attribute_sub4");
            }
            if (item.has("attribute_sub5")) {
                attribute5 = item.getString("attribute_sub5");
            }

            MATEventItem eventItem = new MATEventItem(itemName, quantity, unitPrice, revenue, attribute1,
                    attribute2, attribute3, attribute4, attribute5);
            listItems.add(eventItem);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return listItems;
}

From source file:com.dzt.uberclone.HomeFragment.java

private void showUberMarkers(String json) {
    try {/*from   ww  w. ja v  a 2  s .c  o m*/
        markers.clear();
        shortestTime = 0;
        JSONArray jsonArray = new JSONArray(json);
        nearbyUbers = jsonArray.length();
        ubercount = 0;
        if (nearbyUbers == 0) {
            displayNoUbersMessage();
        }
        for (int i = 0; i < nearbyUbers; i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            double lat = jsonObject.getDouble("pos_lat");
            double lon = jsonObject.getDouble("pos_long");
            addUberMarker(lat, lon);
            getShortestTime(lat, lon);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

From source file:com.dzt.uberclone.HomeFragment.java

private void handleDriverTracking(String json) {
    try {/*from   w  w  w  .jav  a2  s. com*/
        JSONObject jsonObject = new JSONObject(json);
        if (jsonObject.has("fee")) {
            trackDriverBoolean = false;
            double initialLat = jsonObject.getDouble("initial_lat");
            double initialLng = jsonObject.getDouble("initial_lng");
            double finalLat = jsonObject.getDouble("final_lat");
            double finalLng = jsonObject.getDouble("final_lng");
            String distance = jsonObject.getString("distance");
            String time = jsonObject.getString("time");
            String fee = jsonObject.getString("fee");
            String finalFee = jsonObject.getString("final_fee");

            Bundle params = new Bundle();
            params.putString("originText", initialLat + "," + initialLng);
            params.putString("destinationText", finalLat + "," + finalLng);
            params.putString("timeText", time);
            params.putString("distanceText", distance);
            params.putString("feeText", fee);
            params.putString("finalFeeText", finalFee);
            params.putString("rideId", currentRideId);
            /*
                    
            StringBuilder sb = new StringBuilder();
            sb.append("You went from ");
            sb.append(initialLat);
            sb.append(",");
            sb.append(initialLng);
            sb.append(" to ");
            sb.append(finalLat);
            sb.append(",");
            sb.append(finalLng);
            sb.append(". Your time was ");
            sb.append(time);
            sb.append(" minutes and rode a distance of ");
            sb.append(distance);
            sb.append(" KM. Your fee is $");
            sb.append(fee);
            sb.append(" and your adjusted fee is $");
            sb.append(finalFee);
                    
            Log.i("ride details", sb.toString());
            Toast.makeText(getActivity(), sb.toString(), Toast.LENGTH_LONG).show();
                    
            */

            Intent intent = new Intent(getActivity(), RideDetailsActivity.class);
            intent.putExtras(params);
            startActivity(intent);
            getActivity().finish();

        } else {
            double lat = jsonObject.getDouble("latitude");
            double lng = jsonObject.getDouble("longitude");
            addAssignedUberMarker(lat, lng);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}