Example usage for org.json JSONObject optInt

List of usage examples for org.json JSONObject optInt

Introduction

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

Prototype

public int optInt(String key) 

Source Link

Document

Get an optional int value associated with a key, or zero if there is no such key or if the value is not a number.

Usage

From source file:com.tatsuowatanabe.funukulelequiz.util.Purchase.java

public Purchase(String itemType, String jsonPurchaseInfo, String signature) throws JSONException {
    mItemType = itemType;// ww w  . j ava2s  . co  m
    mOriginalJson = jsonPurchaseInfo;
    JSONObject o = new JSONObject(mOriginalJson);
    mOrderId = o.optString("orderId");
    mPackageName = o.optString("packageName");
    mSku = o.optString("productId");
    mPurchaseTime = o.optLong("purchaseTime");
    mPurchaseState = o.optInt("purchaseState");
    mDeveloperPayload = o.optString("developerPayload");
    mToken = o.optString("token", o.optString("purchaseToken"));
    mIsAutoRenewing = o.optBoolean("autoRenewing");
    mSignature = signature;
}

From source file:com.weibo.net.Oauth2AccessToken.java

public Oauth2AccessToken(String rltString) {
    // { "access_token":"SlAV32hkKG", "expires_in":3600, "refresh_token":"8xLOxBtZp8" } 
    if (rltString != null) {
        if (rltString.indexOf("{") >= 0) {
            try {
                JSONObject json = new JSONObject(rltString);
                setToken(json.optString("access_token"));
                setExpiresIn(json.optInt("expires_in"));
                setRefreshToken(json.optString("refresh_token"));
            } catch (JSONException e) {
                //??
            }//from   w w w .  j a va2s  . co  m
        }
    }
}

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

/**
 * Fills a City instance from JSONObject.
 *///ww w.  j  ava  2s.co  m
public VKApiCity parse(JSONObject from) {
    id = from.optInt("id");
    title = from.optString("title");
    return this;
}

From source file:org.akop.ararat.io.WSJFormatter.java

@Override
public void read(Crossword.Builder builder, InputStream inputStream) throws IOException {
    InputStreamReader reader = new InputStreamReader(inputStream, mEncoding);

    StringBuilder sb = new StringBuilder();
    int nread;// w  w w .  j  a  va2  s.  com
    char[] buffer = new char[4000];
    while ((nread = reader.read(buffer, 0, buffer.length)) > -1) {
        sb.append(buffer, 0, nread);
    }

    JSONObject obj;
    try {
        obj = new JSONObject(sb.toString());
    } catch (JSONException e) {
        throw new FormatException("Error parsing JSON object", e);
    }

    JSONObject dataObj = obj.optJSONObject("data");
    if (dataObj == null) {
        throw new FormatException("Missing 'data'");
    }

    JSONObject copyObj = dataObj.optJSONObject("copy");
    if (copyObj == null) {
        throw new FormatException("Missing 'data.copy'");
    }

    JSONObject gridObj = copyObj.optJSONObject("gridsize");
    if (gridObj == null) {
        throw new FormatException("Missing 'data.copy.gridsize'");
    }

    builder.setTitle(copyObj.optString("title"));
    builder.setDescription(copyObj.optString("description"));
    builder.setCopyright(copyObj.optString("publisher"));
    builder.setAuthor(copyObj.optString("byline"));

    String pubString = copyObj.optString("date-publish");
    try {
        builder.setDate(PUBLISH_DATE_FORMAT.parse(pubString).getTime());
    } catch (ParseException e) {
        throw new FormatException("Can't parse '" + pubString + "' as publish date");
    }

    int width = gridObj.optInt("cols");
    int height = gridObj.optInt("rows");

    builder.setWidth(width);
    builder.setHeight(height);

    readClues(builder, copyObj, Grid.parseJSON(dataObj.optJSONArray("grid"), width, height));
}

From source file:com.appsimobile.appsii.module.weather.ImageDownloadHelper.java

static String urlFromImageObject(JSONObject object, String u, String w, String h, int minDimension) {
    String url = object.optString(u);
    if (TextUtils.isEmpty(url))
        return null;
    int heightO = object.optInt(h);
    int widthO = object.optInt(w);
    if (heightO < minDimension || widthO < minDimension)
        return null;
    return url;/*from w w w.  j  a  va2 s  .  c o m*/
}

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

/**
 * Fills an owner from JSONObject/* w w  w. j a  va  2s  .  c o m*/
 */
public VKApiOwner parse(JSONObject from) {
    fields = from;
    id = from.optInt("id");
    return this;
}

From source file:org.catnut.metadata.User.java

@Override
public ContentValues convert(JSONObject json) {
    ContentValues user = new ContentValues();
    user.put(BaseColumns._ID, json.optLong(Constants.ID));
    user.put(screen_name, json.optString(screen_name));
    user.put(name, json.optString(name));
    user.put(province, json.optInt(province));
    user.put(city, json.optInt(city));/*w  ww .j  ava 2  s .  c  o  m*/
    user.put(location, json.optString(location));
    user.put(description, json.optString(description));
    user.put(url, json.optString(url));
    user.put(profile_image_url, json.optString(profile_image_url));
    user.put(cover_image, json.optString(cover_image));
    user.put(cover_image_phone, json.optString(cover_image_phone));
    user.put(profile_url, json.optString(profile_url));
    user.put(domain, json.optString(domain));
    user.put(weihao, json.optString(weihao));
    user.put(gender, json.optString(gender));
    user.put(followers_count, json.optInt(followers_count));
    user.put(friends_count, json.optInt(friends_count));
    user.put(statuses_count, json.optInt(statuses_count));
    user.put(favourites_count, json.optInt(favourites_count));
    user.put(created_at, json.optString(created_at));
    user.put(following, json.optBoolean(following));
    user.put(allow_all_act_msg, json.optBoolean(allow_all_act_msg));
    user.put(geo_enabled, json.optBoolean(geo_enabled));
    user.put(verified, json.optBoolean(verified));
    user.put(verified_type, json.optInt(verified_type));
    user.put(remark, json.optString(remark));
    //      user.put(ptype, json.optInt(ptype));
    user.put(allow_all_comment, json.optBoolean(allow_all_comment));
    user.put(avatar_large, json.optString(avatar_large));
    user.put(avatar_hd, json.optString(avatar_hd));
    user.put(verified_reason, json.optString(verified_reason));
    user.put(follow_me, json.optBoolean(follow_me));
    user.put(online_status, json.optInt(online_status));
    user.put(bi_followers_count, json.optInt(bi_followers_count));
    user.put(lang, json.optString(lang));
    //      user.put(star, json.optString(star));
    //      user.put(mbtype, json.optInt(mbtype));
    //      user.put(mbrank, json.optInt(mbrank));
    //      user.put(block_word, json.optInt(block_word));
    // ?id
    if (json.has(SINGLE)) {
        user.put(status_id, json.optJSONObject(Status.SINGLE).optLong(Constants.ID));
    }
    return user;
}

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

public static Field fromJson(SymbolTable symbolTable, JSONObject fieldJson) {
    String type = fieldJson.optString("type");
    if (type == null || !type.equals("money"))
        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;
    double defaultValue = fieldJson.has("default_value") ? fieldJson.optDouble("default_value") : 0;
    double minValue = fieldJson.has("min_value") ? fieldJson.optDouble("min_value") : Double.MIN_VALUE;
    double maxValue = fieldJson.has("max_value") ? fieldJson.optDouble("max_value") : Double.MAX_VALUE;
    int nominalWidth = fieldJson.has("nominal_width") ? fieldJson.optInt("nominal_width") : 0;
    String compute = fieldJson.has("compute") ? fieldJson.optString("compute") : null;
    return new MoneyField(symbolTable, name, label, description, defaultValue, minValue, maxValue, nominalWidth,
            compute);/*  w  w w . ja  v a2  s. c o  m*/
}

From source file:com.nascent.android.glass.glasshackto.greenpfinder.model.GreenPSpots.java

/**
 * Converts a JSON object that represents a place into a {@link Place} object.
 *//*from  w ww.  j  a v a2 s. co m*/
private ParkingLot jsonObjectToParkingLot(JSONObject object) {
    int id = object.optInt("id");
    String name = object.optString("address");
    String rate = object.optString("rate");
    double latitude = object.optDouble("lat", Double.NaN);
    double longitude = object.optDouble("lng", Double.NaN);

    if (!name.isEmpty() && !Double.isNaN(latitude) && !Double.isNaN(longitude)) {
        return new ParkingLot(id, latitude, longitude, name, rate);
    } else {
        return null;
    }
}

From source file:com.mindmeapp.extensions.ExtensionData.java

/**
 * Deserializes the given JSON representation of extension data, populating this
 * object./*from   w w w.j  ava  2 s  .c  o  m*/
 */
public void deserialize(JSONObject data) throws JSONException {
    this.mVisible = data.optBoolean(KEY_VISIBLE);
    this.mIcon = data.optInt(KEY_ICON);
    String iconUriString = data.optString(KEY_ICON_URI);
    this.mIconUri = TextUtils.isEmpty(iconUriString) ? null : Uri.parse(iconUriString);
    this.mStatusToDisplay = data.optString(KEY_STATUS_TO_DISPLAY);
    this.mStatusToSpeak = data.optString(KEY_STATUS_TO_SPEAK);
    this.mContentDescription = data.optString(KEY_CONTENT_DESCRIPTION);
    this.mBackground = data.optInt(KEY_BACKGROUND);
    String backgroundUriString = data.optString(KEY_BACKGROUND_URI);
    this.mBackgroundUri = TextUtils.isEmpty(backgroundUriString) ? null : Uri.parse(backgroundUriString);

    //Build back the Locale object
    String language = data.optString(KEY_LOCALE_LANGUAGE);
    String country = data.optString(KEY_LOCALE_COUNTRY);
    if (TextUtils.isEmpty(country)) {
        this.mLanguageToSpeak = new Locale(language);
    } else {
        this.mLanguageToSpeak = new Locale(language, country);
    }

}