canonical example by Tabnine
public void accessingJson(JSONObject json) { Object invalid = json.get("invalid"); // throws JSONException - "invalid" entry doesn't exists String name = json.getString("name"); // name = "John Brown" int number = json.getInt("name"); // throws JSONException - "name" entry isn't int int age = json.optInt("age", 42); // using default value instead of throwing exception JSONArray pets = json.getJSONArray("pets"); for (int i = 0; i < pets.length(); i++) { String pet = pets.getString(i); } }
/** * Get extra int value. * @param key The key of the stored value. * @param defaultValue The value to return if the key does not exist or the value cannot be * coerced into the necessary type. * @return The stored value or the provided default if it doesn't exist in specified form. */ public int optExtraInt(String key, int defaultValue) { return mExtraData.optInt(key, defaultValue); }
/** * Get extra int value. * @param key The key of the stored value. * @return The stored value or 0 if it doesn't exist in specified form. */ public int optExtraInt(String key) { return mExtraData.optInt(key); }
/** * Get extra int value. * @param key The key of the stored value. * @param defaultValue The value to return if the key does not exist or the value cannot be * coerced into the necessary type. * @return The stored value or the provided default if it doesn't exist in specified form. */ public int optExtraInt(String key, int defaultValue) { return mExtraData.optInt(key, defaultValue); }
/** * Get extra int value. * @param key The key of the stored value. * @return The stored value or 0 if it doesn't exist in specified form. */ public int optExtraInt(String key) { return mExtraData.optInt(key); }
/** * 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. If the value is a string, an * attempt will be made to evaluate it as a number. * * @param key * A key string. * @return An object which is the value. */ public int optInt(String key) { return this.optInt(key, 0); }
/** * Returns the value mapped by {@code name} if it exists and is an int or * can be coerced to an int. Returns 0 otherwise. */ public int optInt(String name) { return optInt(name, 0); }
/** * Returns the value mapped by {@code name} if it exists and is an int or can be coerced to an * int, or 0 otherwise. * * @param name The name of the field we want. * @return The selected value if it exists. */ public int optInt(String name) { return optInt(name, 0); }
public int getInt(String field) { return jsonData.optInt(field); }
/** * 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. If the value is a string, an * attempt will be made to evaluate it as a number. * * @param key * A key string. * @return An object which is the value. */ public int optInt(String key) { return this.optInt(key, 0); }
public int getInt(String key) { return jsonObject.optInt(key); }
PathComponent(final JSONObject component) throws JSONException { className = component.getString(PATH_CLASS_NAME_KEY); index = component.optInt(PATH_INDEX_KEY, -1); id = component.optInt(PATH_ID_KEY); text = component.optString(PATH_TEXT_KEY); tag = component.optString(PATH_TAG_KEY); description = component.optString(PATH_DESCRIPTION_KEY); hint = component.optString(PATH_HINT_KEY); matchBitmask = component.optInt(PATH_MATCH_BITMASK_KEY); } }
private static List<String> extractValidValues( Context context, JSONObject jsonObject, int keysId, int labelsId) { List<String> labels = new ArrayList<>(); if (jsonObject != null) { String[] keys = context.getResources().getStringArray(keysId); String[] labelArray = context.getResources().getStringArray(labelsId); for (int i=0; i<keys.length; i++) { int value = jsonObject.optInt(keys[i]); if (value == 1) { labels.add(labelArray[i]); } } } return labels; }
@Override public void onCompleted(GraphResponse response) { JSONObject data = response.getJSONObject(); if (data == null) { return; } refreshResult.accessToken = data.optString("access_token"); refreshResult.expiresAt = data.optInt("expires_at"); refreshResult.dataAccessExpirationTime = data.optLong("data_access_expiration_time"); } })
@Override public void parseWith(JSONObject data) { super.parseWith(data); if (data != null) { column = data.optInt(KEY_COLUMN, 2); vGap = hGap = Style.parseSize(data.optString(KEY_GAP), 0); hGap = Style.parseSize(data.optString(KEY_H_GAP), 0); vGap = Style.parseSize(data.optString(KEY_V_GAP), 0); } } }
@Override protected CharSequence getSubTitleOfGraphObject(JSONObject graphObject) { String category = graphObject.optString(CATEGORY); int wereHereCount = graphObject.optInt(WERE_HERE_COUNT); String result = null; if (category != null && wereHereCount != 0) { result = getString(R.string.picker_placepicker_subtitle_format, category, wereHereCount); } else if (category == null && wereHereCount != 0) { result = getString(R.string.picker_placepicker_subtitle_were_here_only_format, wereHereCount); } else if (category != null && wereHereCount == 0) { result = getString(R.string.picker_placepicker_subtitle_catetory_only_format, category); } return result; }
@Override protected void performAsserts(GraphResponse response) { assertNotNull(response); JSONObject retrievedPhoto = response.getJSONObject(); assertNotNull(retrievedPhoto); assertEquals(image1Size, retrievedPhoto.optInt("width")); } });
@Override protected void performAsserts(GraphResponse response) { assertNotNull(response); JSONObject retrievedPhoto = response.getJSONObject(); assertNotNull(retrievedPhoto); assertEquals(image2Size, retrievedPhoto.optInt("width")); } });