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:com.mygdx.game.multiplayer.World.java

private void generateLevel() {
    try {// w  w  w  .j a  v  a  2  s  .  c o  m
        JSONArray array = new JSONArray(Assets.platformDataString);
        int i = 0;
        float y = Platform.PLATFORM_HEIGHT / 2;
        float maxJumpHeight = Bob.BOB_JUMP_VELOCITY * Bob.BOB_JUMP_VELOCITY / (2 * -gravity.y);
        while (y < WORLD_HEIGHT - WORLD_WIDTH / 2) {
            int type = rand.nextFloat() > 0.8f ? Platform.PLATFORM_TYPE_MOVING : Platform.PLATFORM_TYPE_STATIC;
            float x = rand.nextFloat() * (WORLD_WIDTH - Platform.PLATFORM_WIDTH) + Platform.PLATFORM_WIDTH / 2;
            if (i >= array.length()) {
                i = array.length() - 1;
            }
            JSONObject data = array.getJSONObject(i);
            i++;
            Platform platform = new Platform(data.getInt("type"), (float) data.getDouble("x"),
                    (float) data.getDouble("y"));
            platforms.add(platform);

            if (rand.nextFloat() > 0.9f && type != Platform.PLATFORM_TYPE_MOVING) {
                Spring spring = new Spring(platform.position.x,
                        platform.position.y + Platform.PLATFORM_HEIGHT / 2 + Spring.SPRING_HEIGHT / 2);
                springs.add(spring);
            }

            if (y > WORLD_HEIGHT / 3 && rand.nextFloat() > 0.8f) {
                Squirrel squirrel = new Squirrel(platform.position.x + rand.nextFloat(),
                        platform.position.y + Squirrel.SQUIRREL_HEIGHT + rand.nextFloat() * 2);
                squirrels.add(squirrel);
            }

            if (rand.nextFloat() > 0.6f) {
                Coin coin = new Coin(platform.position.x + rand.nextFloat(),
                        platform.position.y + Coin.COIN_HEIGHT + rand.nextFloat() * 3);
                coins.add(coin);
            }

            y += (maxJumpHeight - 0.5f);
            y -= rand.nextFloat() * (maxJumpHeight / 3);
        }

        castle = new Castle(WORLD_WIDTH / 2, y);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.nginious.http.serialize.JsonDeserializer.java

/**
 * Deserializes property with the specified name from the specified json object into a double.
 * //from w  w  w  .j a v  a 2s.  c  o  m
 * @param object the json object
 * @param name the property name
 * @return the deserialized double or <code>0.0d</code> if property doesn't exist
 * @throws SerializerException if unable to deserialize value
 */
protected double deserializeDouble(JSONObject object, String name) throws SerializerException {
    try {
        if (object.has(name)) {
            return object.getDouble(name);
        }

        return 0.0d;
    } catch (JSONException e) {
        throw new SerializerException("Can't deserialize double property " + name, e);
    }
}

From source file:com.nginious.http.serialize.JsonDeserializer.java

/**
 * Deserializes property with the specified name from the specified json object into a float.
 * /*from   ww  w .j  av  a2 s.c  om*/
 * @param object the json object
 * @param name the property name
 * @return the deserialized float or <code>0.0f</code> if property doesn't exist
 * @throws SerializerException if unable to deserialize value
 */
protected float deserializeFloat(JSONObject object, String name) throws SerializerException {
    try {
        if (object.has(name)) {
            return (float) object.getDouble(name);
        }

        return 0.0f;
    } catch (JSONException e) {
        throw new SerializerException("Can't deserialize float property " + name, e);
    }
}

From source file:com.ibm.iot.auto.bluemix.samples.ui.DriverService.java

public List<DriverBehaviorDetail> getAllDriverBehaviorDetails(String tripUuId)
        throws IOException, JSONException {
    JSONObject jsonObject = connection.getJSONObject("/drbresult/trip", "trip_uuid=" + tripUuId);
    JSONArray ctxSubTrips = jsonObject.getJSONArray("ctx_sub_trips");
    List<DriverBehaviorDetail> driverBehaviorDetails = new ArrayList<DriverBehaviorDetail>();
    for (int i = 0; i < ctxSubTrips.length(); i++) {
        JSONObject ctxSubTrip = ctxSubTrips.getJSONObject(i);
        JSONArray features = ctxSubTrip.getJSONArray("ctx_features");
        List<ContextFeature> contextFeatures = new ArrayList<ContextFeature>();
        for (int j = 0; j < features.length(); j++) {
            JSONObject feature = features.getJSONObject(j);
            String category = feature.getString("context_category");
            String name = feature.getString("context_name");
            ContextFeature contextFeature = new ContextFeature(category, name);
            contextFeatures.add(contextFeature);
        }/*from   w w w . ja  va2  s.  c o  m*/
        JSONArray details = ctxSubTrip.getJSONArray("driving_behavior_details");
        for (int k = 0; k < details.length(); k++) {
            JSONObject detail = details.getJSONObject(k);
            DriverBehaviorDetail driverBehaviorDetail = new DriverBehaviorDetail(contextFeatures,
                    detail.getString("behavior_name"), String.valueOf(detail.getDouble("start_latitude")),
                    String.valueOf(detail.getDouble("start_longitude")),
                    String.valueOf(detail.getDouble("end_latitude")),
                    String.valueOf(detail.getDouble("end_longitude")),
                    String.valueOf(detail.getLong("start_time")), String.valueOf(detail.getLong("end_time")));
            driverBehaviorDetails.add(driverBehaviorDetail);
        }
    }
    return driverBehaviorDetails;
}

From source file:com.github.mhendred.face4j.model.Face.java

public Face(JSONObject jObj) throws JSONException {
    tid = jObj.getString("tid");
    label = jObj.optString("label");

    confirmed = jObj.getBoolean("confirmed");
    manual = jObj.getBoolean("manual");

    width = (float) jObj.getDouble("width");
    height = (float) jObj.getDouble("height");

    yaw = (float) jObj.getDouble("yaw");
    roll = (float) jObj.getDouble("roll");
    pitch = (float) jObj.getDouble("pitch");

    threshold = jObj.optInt("threshold");

    center = fromJson(jObj.optJSONObject("center"));

    leftEye = fromJson(jObj.optJSONObject("eye_left"));
    rightEye = fromJson(jObj.optJSONObject("eye_right"));

    leftEar = fromJson(jObj.optJSONObject("ear_left"));
    rightEar = fromJson(jObj.optJSONObject("ear_right"));

    chin = fromJson(jObj.optJSONObject("chin"));

    mouthCenter = fromJson(jObj.optJSONObject("mouth_center"));
    mouthRight = fromJson(jObj.optJSONObject("mouth_right"));
    mouthLeft = fromJson(jObj.optJSONObject("mouth_left"));

    nose = fromJson(jObj.optJSONObject("nose"));

    guesses = Guess.fromJsonArray(jObj.optJSONArray("uids"));

    // Attributes
    jObj = jObj.getJSONObject("attributes");

    if (jObj.has("smiling"))
        smiling = jObj.getJSONObject("smiling").getBoolean("value");

    if (jObj.has("glasses"))
        glasses = jObj.getJSONObject("glasses").getBoolean("value");

    if (jObj.has("gender"))
        gender = Gender.valueOf(jObj.getJSONObject("gender").getString("value"));

    if (jObj.has("mood"))
        mood = jObj.getJSONObject("mood").getString("value");

    if (jObj.has("lips"))
        lips = jObj.getJSONObject("lips").getString("value");

    if (jObj.has("age-est"))
        ageEst = jObj.getJSONObject("age-est").getInt("vaule");

    if (jObj.has("age-min"))
        ageMin = jObj.getJSONObject("age-min").getInt("vaule");

    if (jObj.has("age-max"))
        ageMax = jObj.getJSONObject("age-max").getInt("vaule");

    faceConfidence = jObj.getJSONObject("face").getInt("confidence");

    faceRect = new Rect(center, width, height);
}

From source file:com.github.cambierr.lorawanpacket.semtech.Stat.java

public Stat(JSONObject _json) throws MalformedPacketException {

    /**//from w ww. j ava 2 s. com
     * time
     */
    if (!_json.has("time")) {
        throw new MalformedPacketException("missing time");
    } else {
        time = _json.getString("time");
    }

    /**
     * lati
     */
    if (!_json.has("lati")) {
        lati = Double.MAX_VALUE;
    } else {
        lati = _json.getDouble("lati");
    }

    /**
     * longi
     */
    if (!_json.has("longi")) {
        lati = Double.MAX_VALUE;
    } else {
        longi = _json.getDouble("longi");
    }

    /**
     * alti
     */
    if (!_json.has("alti")) {
        alti = Integer.MAX_VALUE;
    } else {
        alti = _json.getInt("alti");
    }

    /**
     * rxnb
     */
    if (!_json.has("rxnb")) {
        rxnb = Integer.MAX_VALUE;
    } else {
        rxnb = _json.getInt("rxnb");
    }

    /**
     * rxok
     */
    if (!_json.has("rxok")) {
        rxok = Integer.MAX_VALUE;
    } else {
        rxok = _json.getInt("rxok");
    }

    /**
     * rxfw
     */
    if (!_json.has("rxfw")) {
        rxfw = Integer.MAX_VALUE;
    } else {
        rxfw = _json.getInt("rxfw");
    }

    /**
     * ackr
     */
    if (!_json.has("ackr")) {
        ackr = Integer.MAX_VALUE;
    } else {
        ackr = _json.getInt("ackr");
    }

    /**
     * dwnb
     */
    if (!_json.has("dwnb")) {
        dwnb = Integer.MAX_VALUE;
    } else {
        dwnb = _json.getInt("dwnb");
    }

    /**
     * txnb
     */
    if (!_json.has("txnb")) {
        txnb = Integer.MAX_VALUE;
    } else {
        txnb = _json.getInt("txnb");
    }

}

From source file:fi.iki.dezgeg.matkakorttiwidget.matkakortti.MatkakorttiApi.java

private Card createCardFromJSON(JSONObject card) throws JSONException {
    double moneyAsDouble = card.getDouble("RemainingMoney");
    // Round to BigDecimal cents safely
    BigDecimal money = new BigDecimal((int) Math.round(100 * moneyAsDouble)).divide(new BigDecimal(100));

    String expiryDateStr = card.getJSONObject("PeriodProductState").getString("ExpiringDate");
    Date expiryDate = null;/*  w w w .j a v a 2 s  .c o m*/
    if (!expiryDateStr.equals("null")) {
        Matcher expiryDateMatch = DATE_PATTERN.matcher(expiryDateStr);
        if (!expiryDateMatch.matches()) {
            throw new MatkakorttiException("Date pattern did not match regex: " + expiryDateStr, true);
        }
        expiryDate = new Date(Long.parseLong(expiryDateMatch.group(1)));
    }

    return new Card(card.getString("name"), card.getString("id"), money, expiryDate);
}

From source file:fr.openbike.android.io.RemoteNetworksHandler.java

@Override
public Object parse(JSONObject json, OpenBikeDBAdapter dbAdapter) throws JSONException, IOException {
    JSONArray jsonNetworks = json.getJSONArray("networks");
    ArrayList<Network> networks = new ArrayList<Network>();
    JSONObject jsonNetwork;
    for (int i = 0; i < jsonNetworks.length(); i++) {
        jsonNetwork = jsonNetworks.getJSONObject(i);
        networks.add(new Network(jsonNetwork.getInt(Network.ID), jsonNetwork.getString(Network.NAME),
                jsonNetwork.getString(Network.CITY), jsonNetwork.getString(Network.SERVER),
                jsonNetwork.getString(Network.SPECIAL_NAME), jsonNetwork.getDouble(Network.LONGITUDE),
                jsonNetwork.getDouble(Network.LATITUDE)));
    }//  ww w.  j  a  v a  2s . c  o  m
    return networks;
}

From source file:jessmchung.groupon.parsers.DealOptionsParser.java

@Override
public DealOptions parse(JSONObject json) throws JSONException {
    DealOptions obj = new DealOptions();
    if (json.has("buyUrl"))
        obj.setBuyUrl(json.getString("buyUrl"));
    if (json.has("expiresAt")) {
        try {// w  ww . j a  v a  2  s .c  om
            obj.setExpiresAt(parseDate(json.getString("expiresAt")));
        } catch (ParseException ex) {
            System.out.println("Could not parse " + json.getString("expiresAt"));
        }
    }
    if (json.has("price")) {
        JSONObject price = json.getJSONObject("price");
        obj.setPrice(new PriceParser().parse(price));
    }
    if (json.has("discountPercent"))
        obj.setDiscountPercent(json.getDouble("discountPercent"));
    if (json.has("soldQuantity"))
        obj.setSoldQuantity(json.getInt("soldQuantity"));
    if (json.has("initialQuantity") && !json.isNull("initialQuantity"))
        obj.setInitialQuantity(json.getInt("initialQuantity"));
    if (json.has("externalUrl"))
        obj.setExternalUrl(json.getString("externalUrl"));
    if (json.has("minimumPurchaseQuantity"))
        obj.setMinimumPurchaseQuantity(json.getInt("minimumPurchaseQuantity"));
    if (json.has("limitedQuantity"))
        obj.setIsLimitedQuantity(json.getBoolean("isLimitedQuantity"));
    if (json.has("value")) {
        JSONObject value = json.getJSONObject("value");
        obj.setValue(new PriceParser().parse(value));
    }
    if (json.has("maximumPurchaseQuantity"))
        obj.setMaximumPurchaseQuantity(json.getInt("maximumPurchaseQuantity"));
    if (json.has("title"))
        obj.setTitle(json.getString("title"));
    if (json.has("discount")) {
        JSONObject discount = json.getJSONObject("discount");
        obj.setValue(new PriceParser().parse(discount));
    }
    if (json.has("remainingQuantity") && !json.isNull("remainingQuantity"))
        obj.setRemainingQuantity(json.getInt("remainingQuantity"));
    if (json.has("id"))
        obj.setId(json.getInt("id"));
    if (json.has("isSoldOut"))
        obj.setIsSoldOut(json.getBoolean("isSoldOut"));
    if (json.has("redemptionLocations")) {
        JSONArray locationsArray = json.getJSONArray("redemptionLocations");
        obj.setRedemptionLocations(new RedemptionLocationParser().parse(locationsArray));
    }

    return obj;
}

From source file:ru.jkff.antro.ReportReader.java

private Stat toStat(JSONObject stat) throws JSONException {
    double avg = stat.getDouble("avg");
    double min = stat.getDouble("min");
    double max = stat.getDouble("max");
    double first = stat.getDouble("first");
    double total = stat.getDouble("total");
    int count = stat.getInt("count");

    PersistentStack<Call> smin = toStack(stat.getJSONObject("evMin").getJSONArray("stack"));
    PersistentStack<Call> smax = toStack(stat.getJSONObject("evMax").getJSONArray("stack"));
    PersistentStack<Call> sfirst = toStack(stat.getJSONObject("evFirst").getJSONArray("stack"));

    EventWithCallStack evMin = new EventWithCallStack(smin);
    EventWithCallStack evMax = new EventWithCallStack(smax);
    EventWithCallStack evFirst = new EventWithCallStack(sfirst);

    return new Stat(avg, min, max, first, total, count, evMin, evMax, evFirst);
}