Example usage for org.json JSONObject optString

List of usage examples for org.json JSONObject optString

Introduction

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

Prototype

public String optString(String key) 

Source Link

Document

Get an optional string associated with a key.

Usage

From source file:net.digitalphantom.app.weatherapp.data.Channel.java

@Override
public void populate(JSONObject data) {

    units = new Units();
    units.populate(data.optJSONObject("units"));

    item = new Item();
    item.populate(data.optJSONObject("item"));

    JSONObject locationData = data.optJSONObject("location");

    String region = locationData.optString("region");
    String country = locationData.optString("country");

    location = String.format("%s, %s", locationData.optString("city"),
            (region.length() != 0 ? region : country));
}

From source file:com.basetechnology.s0.agentserver.field.BooleanField.java

public static Field fromJson(SymbolTable symbolTable, JSONObject fieldJson) {
    String type = fieldJson.optString("type");
    if (type == null || !(type.equals("option") || type.equals("boolean")))
        return null;
    String name = fieldJson.has("name") ? fieldJson.optString("name") : null;
    String label = fieldJson.has("label") ? fieldJson.optString("label") : null;
    String description = fieldJson.has("description") ? fieldJson.optString("description") : null;
    boolean defaultValue = fieldJson.has("default_value") ? fieldJson.optBoolean("default_value") : true;
    String compute = fieldJson.has("compute") ? fieldJson.optString("compute") : null;
    return new BooleanField(symbolTable, name, label, description, defaultValue, compute);
}

From source file:org.changhong.Note.java

public Note(JSONObject json) {

    // These methods return an empty string if the key is not found
    setTitle(XmlUtils.unescape(json.optString("title")));
    setGuid(json.optString("guid"));
    setLastChangeDate(json.optString("last-change-date"));
    String newXMLContent = json.optString("note-content");
    setXmlContent(newXMLContent);/*from w  ww  . j  ava2s . c  o m*/
    JSONArray jtags = json.optJSONArray("tags");
    String tag;
    tags = new String();
    if (jtags != null) {
        for (int i = 0; i < jtags.length(); i++) {
            tag = jtags.optString(i);
            tags += tag + ",";
        }
    }
}

From source file:com.sina.weibo.sdk_lib.openapi.models.Poi.java

public static Poi parse(JSONObject jsonObject) {
    if (null == jsonObject) {
        return null;
    }//from w ww  .  j a  v  a 2  s  .co  m

    Poi poi = new Poi();
    poi.poiid = jsonObject.optString("poiid");
    poi.title = jsonObject.optString("title");
    poi.address = jsonObject.optString("address");
    poi.lon = jsonObject.optString("lon");
    poi.lat = jsonObject.optString("lat");
    poi.category = jsonObject.optString("category");
    poi.city = jsonObject.optString("city");
    poi.province = jsonObject.optString("province");
    poi.country = jsonObject.optString("country");
    poi.url = jsonObject.optString("url");
    poi.phone = jsonObject.optString("phone");
    poi.postcode = jsonObject.optString("postcode");
    poi.weibo_id = jsonObject.optString("weibo_id");
    poi.categorys = jsonObject.optString("categorys");
    poi.category_name = jsonObject.optString("category_name");
    poi.icon = jsonObject.optString("icon");
    poi.checkin_num = jsonObject.optString("checkin_num");
    poi.checkin_user_num = jsonObject.optString("checkin_user_num");
    poi.tip_num = jsonObject.optString("tip_num");
    poi.photo_num = jsonObject.optString("photo_num");
    poi.todo_num = jsonObject.optString("todo_num");
    poi.distance = jsonObject.optString("distance");

    return poi;
}

From source file:mc.inappbilling.Purchase.java

public Purchase(String json, String signature) throws JSONException {
    this.json = json;
    JSONObject o = new JSONObject(json);
    this.orderId = o.optString("orderId");
    this.packageName = o.optString("packageName");
    this.sku = o.optString("productId");
    this.purchaseTime = o.optLong("purchaseTime");
    this.purchaseState = o.optInt("purchaseState");
    this.developerPayload = o.optString("developerPayload");
    this.token = o.optString("token", o.optString("purchaseToken"));
    this.signature = signature;
}

From source file:com.xgf.inspection.qrcode.google.zxing.client.result.supplement.BookResultInfoRetriever.java

@Override
void retrieveSupplementalInfo() throws IOException {

    CharSequence contents = HttpHelper.downloadViaHttp(
            "https://www.googleapis.com/books/v1/volumes?q=isbn:" + isbn, HttpHelper.ContentType.JSON);

    if (contents.length() == 0) {
        return;//from www.  j a  v a  2 s .  c o m
    }

    String title;
    String pages;
    Collection<String> authors = null;

    try {

        JSONObject topLevel = (JSONObject) new JSONTokener(contents.toString()).nextValue();
        JSONArray items = topLevel.optJSONArray("items");
        if (items == null || items.isNull(0)) {
            return;
        }

        JSONObject volumeInfo = ((JSONObject) items.get(0)).getJSONObject("volumeInfo");
        if (volumeInfo == null) {
            return;
        }

        title = volumeInfo.optString("title");
        pages = volumeInfo.optString("pageCount");

        JSONArray authorsArray = volumeInfo.optJSONArray("authors");
        if (authorsArray != null && !authorsArray.isNull(0)) {
            authors = new ArrayList<String>(authorsArray.length());
            for (int i = 0; i < authorsArray.length(); i++) {
                authors.add(authorsArray.getString(i));
            }
        }

    } catch (JSONException e) {
        throw new IOException(e.toString());
    }

    Collection<String> newTexts = new ArrayList<String>();

    if (title != null && title.length() > 0) {
        newTexts.add(title);
    }

    if (authors != null && !authors.isEmpty()) {
        boolean first = true;
        StringBuilder authorsText = new StringBuilder();
        for (String author : authors) {
            if (first) {
                first = false;
            } else {
                authorsText.append(", ");
            }
            authorsText.append(author);
        }
        newTexts.add(authorsText.toString());
    }

    if (pages != null && pages.length() > 0) {
        newTexts.add(pages + "pp.");
    }

    String baseBookUri = "http://www.google." + LocaleManager.getBookSearchCountryTLD(context)
            + "/search?tbm=bks&source=zxing&q=";

    append(isbn, source, newTexts.toArray(new String[newTexts.size()]), baseBookUri + isbn);
}

From source file:org.uiautomation.ios.server.command.web.TapHandler.java

public TapHandler(IOSServerManager driver, WebDriverLikeRequest request) throws Exception {
    super(driver, request);

    JSONObject payload = request.getPayload();
    String elementId = payload.optString("element");

    //String ref = request.getVariableValue(":reference");
    Dimension screenSize = driver.getSession(request.getSession()).getNativeDriver().getScreenSize();
    RemoteWebNativeBackedElement element = (RemoteWebNativeBackedElement) getSession().getRemoteWebDriver()
            .createElement(elementId);// ww w .  ja  va2 s . co m
    Point tapPoint = element.getLocation();
    tapPoint = CoordinateUtils.forcePointOnScreen(tapPoint, screenSize);

    String js = tapTemplate.replace(":sessionId", request.getSession())
            .replace("tapX", Integer.toString(tapPoint.getX()))
            .replace("tapY", Integer.toString(tapPoint.getY()));

    setJS(js);
}

From source file:gov.sfmta.sfpark.MyAnnotation.java

public int iconFinder(boolean showPrice) throws JSONException {
    int itemImageName = 0;
    if (onStreet) {
        return itemImageName;
    }// ww  w  .j a  v a2s  .  c  om

    JSONObject rates = allGarageData.optJSONObject(RATES_KEY);
    String type = allGarageData.optString(TYPE_KEY);
    int used = allGarageData.optInt(OCC_KEY, 0);
    int capacity = allGarageData.optInt(OPER_KEY, 0);
    int avail = capacity - used;
    int availpercent = 0;

    boolean invalidData = true;

    if (capacity == 0) {
        availpercent = 0;
        invalidData = true;
    } else {
        availpercent = Math.round((((float) avail / (float) capacity) * 100) * 10) / 10;
        invalidData = false;
    }

    int usedpercent = 100 - availpercent;

    if (avail < 2 && avail > 0 && availpercent != 0 && capacity <= 3) {
        if (availpercent <= 15) {
            usedpercent = -57; // less than 15 percent available. hack
        } else {
            usedpercent = -58; // more than 15 percent available. hack
        }
    } else if (capacity == 0 && used == 0 && "ON".equals(type)) {
        // On street parking, force it to red as capacity is zero
        usedpercent = -42;
    }

    // since the code above and code below is adapted from
    // two different functions from webmap.js this
    // variable links amountUsed and usedpercent to keep
    // the source code consistent here in the java version
    // where the two functions are basically fused
    // together. This is a hack on a hack. *sigh*
    int amountUsed = usedpercent;
    if (invalidData) {
        itemImageName = invalid_garage;
    } else if (amountUsed > 90 || amountUsed == -42) {
        itemImageName = garage_availability_low;
    } else if ((amountUsed <= 90 && amountUsed >= 70) || amountUsed == -58) {
        itemImageName = garage_availability_medium;
    } else if ((amountUsed < 70 && amountUsed >= 0) || amountUsed == -57) {
        itemImageName = garage_availability_high;
    }

    // modified so that negative available spaces will show as red, not grey:
    if (amountUsed == -1 || amountUsed == -2) {
        itemImageName = invalid_garage;
    }

    // New rule: if available > capacity, show grey not red
    if (avail > capacity)
        itemImageName = invalid_garage;

    // Save garage availability color even if we're on a price page,
    // because we may need it for the details page:
    switch (itemImageName) {
    case garage_availability_high:
        garageColor = availHigh;
        break;
    case garage_availability_medium:
        garageColor = availMed;
        break;
    case garage_availability_low:
        garageColor = availLow;
        break;
    default: // invalid
        garageColor = grey;
        break;
    }

    if (showPrice) {
        if (rates != null) {
            JSONArray rateArray = rates.optJSONArray(RS_KEY);
            if (!(rateArray == null)) {
                boolean isDynamicPricing = true;
                int rsc = rateArray.length();
                for (int i = 0; i < rsc; i++) {
                    JSONObject rateObject = rateArray.getJSONObject(i);
                    float phr = (float) rateObject.optDouble(RATE_KEY, 0);
                    String description = rateObject.optString(DESC_KEY);
                    if (description != null) {
                        if (description.contains(INCREMENTAL_STR)) {
                            itemImageName = nameFinder(phr);
                            isDynamicPricing = false;
                            pricePerHour = phr;
                            break;
                        }
                    }
                }

                if (isDynamicPricing) {
                    for (int i = 0; i < rsc; i++) {
                        JSONObject rateObject = rateArray.getJSONObject(i);
                        float phr = (float) rateObject.optDouble(RATE_KEY, 0);
                        rateStructureHandle(rateObject);
                        if (inThisBucketBegin(beg, end)) {
                            itemImageName = nameFinder(phr);
                            pricePerHour = phr;
                            break;
                        }
                    }
                }
            }
        }
    }
    rateQualifier = rq;
    return itemImageName;
}

From source file:com.vk.sdkweb.api.model.VKApiChat.java

/**
 * Fills a Chat instance from JSONObject.
 */// www  . j  a v  a2 s .c  o m
public VKApiChat parse(JSONObject source) {
    id = source.optInt("id");
    type = source.optString("type");
    title = source.optString("title");
    admin_id = source.optInt("admin_id");
    JSONArray users = source.optJSONArray("users");
    if (users != null) {
        this.users = new int[users.length()];
        for (int i = 0; i < this.users.length; i++) {
            this.users[i] = users.optInt(i);
        }
    }
    return this;
}

From source file:com.google.blockly.model.FieldColor.java

public static FieldColor fromJson(JSONObject json) throws BlockLoadingException {
    String name = json.optString("name");
    if (TextUtils.isEmpty(name)) {
        throw new BlockLoadingException("field_colour \"name\" attribute must not be empty.");
    }//from  w  w  w . j  av  a  2s .  c  o m
    int color = DEFAULT_COLOR;

    String colourString = json.optString("colour");
    if (!TextUtils.isEmpty(colourString)) {
        color = Color.parseColor(colourString);
    }
    return new FieldColor(name, color);
}