本文整理了Java中org.json.JSONObject.optInt()
方法的一些代码示例,展示了JSONObject.optInt()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JSONObject.optInt()
方法的具体详情如下:
包路径:org.json.JSONObject
类名称:JSONObject
方法名:optInt
[英]Returns the value mapped by name if it exists and is an int or can be coerced to an int. Returns 0 otherwise.
[中]返回按名称映射的值(如果该值存在且为int或可以强制为int)。否则返回0。
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);
}
}
代码示例来源:origin: google/physical-web
/**
* 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);
}
代码示例来源:origin: google/physical-web
/**
* 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);
}
代码示例来源:origin: google/physical-web
/**
* 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);
}
代码示例来源:origin: google/physical-web
/**
* 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);
}
代码示例来源:origin: zzz40500/GsonFormat
/**
* 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);
}
代码示例来源:origin: robovm/robovm
/**
* 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);
}
代码示例来源:origin: apache/geode
/**
* 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);
}
代码示例来源:origin: facebook/facebook-android-sdk
public int getInt(String field) {
return jsonData.optInt(field);
}
代码示例来源:origin: loklak/loklak_server
/**
* 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);
}
代码示例来源:origin: apache/geode
public int getInt(String key) {
return jsonObject.optInt(key);
}
代码示例来源:origin: alibaba/Tangram-Android
public int optIntParam(String key) {
if (extras.has(key)) {
return extras.optInt(key);
}
if (style != null && style.extras != null) {
return style.extras.optInt(key);
}
return 0;
}
代码示例来源:origin: alibaba/Tangram-Android
public int optIntParam(String key) {
if (extras.has(key))
return extras.optInt(key);
if (style != null && style.extras != null)
return style.extras.optInt(key);
return 0;
}
代码示例来源:origin: facebook/facebook-android-sdk
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);
}
}
代码示例来源:origin: facebook/facebook-android-sdk
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;
}
代码示例来源:origin: facebook/facebook-android-sdk
@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");
}
})
代码示例来源:origin: alibaba/Tangram-Android
@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);
}
}
}
代码示例来源:origin: facebook/facebook-android-sdk
@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;
}
代码示例来源:origin: facebook/facebook-android-sdk
@Override
protected void performAsserts(GraphResponse response) {
assertNotNull(response);
JSONObject retrievedPhoto = response.getJSONObject();
assertNotNull(retrievedPhoto);
assertEquals(image1Size, retrievedPhoto.optInt("width"));
}
});
代码示例来源:origin: facebook/facebook-android-sdk
@Override
protected void performAsserts(GraphResponse response) {
assertNotNull(response);
JSONObject retrievedPhoto = response.getJSONObject();
assertNotNull(retrievedPhoto);
assertEquals(image2Size, retrievedPhoto.optInt("width"));
}
});
内容来源于网络,如有侵权,请联系作者删除!