本文整理了Java中org.json.JSONObject.stringToNumber()
方法的一些代码示例,展示了JSONObject.stringToNumber()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JSONObject.stringToNumber()
方法的具体详情如下:
包路径:org.json.JSONObject
类名称:JSONObject
方法名:stringToNumber
[英]Converts a string to a number using the narrowest possible type. Possible returns for this function are BigDecimal, Double, BigInteger, Long, and Integer. When a Double is returned, it should always be a valid Double and not NaN or +-infinity.
[中]使用可能的最窄类型将字符串转换为数字。此函数的可能返回值为BigDecimal、Double、BigInteger、Long和Integer。返回Double时,它应该始终是有效的Double,而不是NaN或+-无穷大。
代码示例来源:origin: loklak/loklak_server
/**
* Get the Number value associated with a key.
*
* @param index
* The index must be between 0 and length() - 1.
* @return The numeric value.
* @throws JSONException
* if the key is not found or if the value is not a Number
* object and cannot be converted to a number.
*/
public Number getNumber(int index) throws JSONException {
Object object = this.get(index);
try {
if (object instanceof Number) {
return (Number)object;
}
return JSONObject.stringToNumber(object.toString());
} catch (Exception e) {
throw new JSONException("JSONArray[" + index + "] is not a number.", e);
}
}
代码示例来源:origin: loklak/loklak_server
/**
* Get an optional {@link Number} value associated with a key, or the default 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 ({@link BigDecimal}). This method
* would be used in cases where type coercion of the number value is unwanted.
*
* @param index
* The index must be between 0 and length() - 1.
* @param defaultValue
* The default.
* @return An object which is the value.
*/
public Number optNumber(int index, Number defaultValue) {
Object val = this.opt(index);
if (JSONObject.NULL.equals(val)) {
return defaultValue;
}
if (val instanceof Number){
return (Number) val;
}
if (val instanceof String) {
try {
return JSONObject.stringToNumber((String) val);
} catch (Exception e) {
return defaultValue;
}
}
return defaultValue;
}
代码示例来源:origin: loklak/loklak_server
return stringToNumber((String) val);
} catch (Exception e) {
return defaultValue;
代码示例来源:origin: loklak/loklak_server
/**
* Get the Number value associated with a key.
*
* @param key
* A key string.
* @return The numeric value.
* @throws JSONException
* if the key is not found or if the value is not a Number
* object and cannot be converted to a number.
*/
public Number getNumber(String key) throws JSONException {
Object object = this.get(key);
try {
if (object instanceof Number) {
return (Number)object;
}
return stringToNumber(object.toString());
} catch (Exception e) {
throw new JSONException("JSONObject[" + quote(key)
+ "] is not a number.", e);
}
}
代码示例来源:origin: b3log/latke
/**
* Get the Number value associated with a key.
*
* @param index
* The index must be between 0 and length() - 1.
* @return The numeric value.
* @throws JSONException
* if the key is not found or if the value is not a Number
* object and cannot be converted to a number.
*/
public Number getNumber(int index) throws JSONException {
Object object = this.get(index);
try {
if (object instanceof Number) {
return (Number)object;
}
return JSONObject.stringToNumber(object.toString());
} catch (Exception e) {
throw new JSONException("JSONArray[" + index + "] is not a number.", e);
}
}
代码示例来源:origin: b3log/latke
/**
* Get an optional {@link Number} value associated with a key, or the default 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. This method
* would be used in cases where type coercion of the number value is unwanted.
*
* @param key
* A key string.
* @param defaultValue
* The default.
* @return An object which is the value.
*/
public Number optNumber(String key, Number defaultValue) {
Object val = this.opt(key);
if (NULL.equals(val)) {
return defaultValue;
}
if (val instanceof Number){
return (Number) val;
}
try {
return stringToNumber(val.toString());
} catch (Exception e) {
return defaultValue;
}
}
代码示例来源:origin: b3log/latke
/**
* Get an optional {@link Number} value associated with a key, or the default 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 ({@link BigDecimal}). This method
* would be used in cases where type coercion of the number value is unwanted.
*
* @param index
* The index must be between 0 and length() - 1.
* @param defaultValue
* The default.
* @return An object which is the value.
*/
public Number optNumber(int index, Number defaultValue) {
Object val = this.opt(index);
if (JSONObject.NULL.equals(val)) {
return defaultValue;
}
if (val instanceof Number){
return (Number) val;
}
if (val instanceof String) {
try {
return JSONObject.stringToNumber((String) val);
} catch (Exception e) {
return defaultValue;
}
}
return defaultValue;
}
代码示例来源:origin: b3log/latke
/**
* Get the Number value associated with a key.
*
* @param key
* A key string.
* @return The numeric value.
* @throws JSONException
* if the key is not found or if the value is not a Number
* object and cannot be converted to a number.
*/
public Number getNumber(String key) throws JSONException {
Object object = this.get(key);
try {
if (object instanceof Number) {
return (Number)object;
}
return stringToNumber(object.toString());
} catch (Exception e) {
throw new JSONException("JSONObject[" + quote(key)
+ "] is not a number.", e);
}
}
代码示例来源:origin: org.b3log/latke
/**
* Get the Number value associated with a key.
*
* @param index
* The index must be between 0 and length() - 1.
* @return The numeric value.
* @throws JSONException
* if the key is not found or if the value is not a Number
* object and cannot be converted to a number.
*/
public Number getNumber(int index) throws JSONException {
Object object = this.get(index);
try {
if (object instanceof Number) {
return (Number)object;
}
return JSONObject.stringToNumber(object.toString());
} catch (Exception e) {
throw new JSONException("JSONArray[" + index + "] is not a number.", e);
}
}
代码示例来源:origin: org.b3log/latke
/**
* Get an optional {@link Number} value associated with a key, or the default 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. This method
* would be used in cases where type coercion of the number value is unwanted.
*
* @param key
* A key string.
* @param defaultValue
* The default.
* @return An object which is the value.
*/
public Number optNumber(String key, Number defaultValue) {
Object val = this.opt(key);
if (NULL.equals(val)) {
return defaultValue;
}
if (val instanceof Number){
return (Number) val;
}
try {
return stringToNumber(val.toString());
} catch (Exception e) {
return defaultValue;
}
}
代码示例来源:origin: org.b3log/latke
/**
* Get an optional {@link Number} value associated with a key, or the default 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 ({@link BigDecimal}). This method
* would be used in cases where type coercion of the number value is unwanted.
*
* @param index
* The index must be between 0 and length() - 1.
* @param defaultValue
* The default.
* @return An object which is the value.
*/
public Number optNumber(int index, Number defaultValue) {
Object val = this.opt(index);
if (JSONObject.NULL.equals(val)) {
return defaultValue;
}
if (val instanceof Number){
return (Number) val;
}
if (val instanceof String) {
try {
return JSONObject.stringToNumber((String) val);
} catch (Exception e) {
return defaultValue;
}
}
return defaultValue;
}
代码示例来源:origin: org.b3log/latke
/**
* Get the Number value associated with a key.
*
* @param key
* A key string.
* @return The numeric value.
* @throws JSONException
* if the key is not found or if the value is not a Number
* object and cannot be converted to a number.
*/
public Number getNumber(String key) throws JSONException {
Object object = this.get(key);
try {
if (object instanceof Number) {
return (Number)object;
}
return stringToNumber(object.toString());
} catch (Exception e) {
throw new JSONException("JSONObject[" + quote(key)
+ "] is not a number.", e);
}
}
内容来源于网络,如有侵权,请联系作者删除!