本文整理了Java中org.json.JSONArray.optNumber()
方法的一些代码示例,展示了JSONArray.optNumber()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JSONArray.optNumber()
方法的具体详情如下:
包路径:org.json.JSONArray
类名称:JSONArray
方法名:optNumber
[英]Get an optional Number value associated with a key, or null
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 ( BigDecimal). This method would be used in cases where type coercion of the number value is unwanted.
[中]获取与键关联的可选数字值,如果没有此类键或该值不是数字,则获取null
。如果值是字符串,将尝试将其作为数字(BigDecimal)计算。此方法将用于不需要数字值的类型强制的情况。
代码示例来源:origin: loklak/loklak_server
/**
* Get an optional {@link Number} value associated with a key, or null
* 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.
* @return An object which is the value.
*/
public Number optNumber(int index) {
return this.optNumber(index, null);
}
代码示例来源:origin: b3log/latke
/**
* Get an optional {@link Number} value associated with a key, or null
* 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.
* @return An object which is the value.
*/
public Number optNumber(int index) {
return this.optNumber(index, null);
}
代码示例来源:origin: b3log/latke
/**
* Get the optional int value associated with an index. The defaultValue is
* returned if there is no value for the index, or if the value is not a
* number and cannot be converted to a number.
*
* @param index
* The index must be between 0 and length() - 1.
* @param defaultValue
* The default value.
* @return The value.
*/
public int optInt(int index, int defaultValue) {
final Number val = this.optNumber(index, null);
if (val == null) {
return defaultValue;
}
return val.intValue();
}
代码示例来源:origin: b3log/latke
/**
* Get the optional long value associated with an index. The defaultValue is
* returned if there is no value for the index, or if the value is not a
* number and cannot be converted to a number.
*
* @param index
* The index must be between 0 and length() - 1.
* @param defaultValue
* The default value.
* @return The value.
*/
public long optLong(int index, long defaultValue) {
final Number val = this.optNumber(index, null);
if (val == null) {
return defaultValue;
}
return val.longValue();
}
代码示例来源:origin: b3log/latke
/**
* Get the optional double value associated with an index. The defaultValue
* is returned if there is no value for the index, or if the value is not a
* number and cannot be converted to a number.
*
* @param index
* subscript
* @param defaultValue
* The default value.
* @return The value.
*/
public double optDouble(int index, double defaultValue) {
final Number val = this.optNumber(index, null);
if (val == null) {
return defaultValue;
}
final double doubleValue = val.doubleValue();
// if (Double.isNaN(doubleValue) || Double.isInfinite(doubleValue)) {
// return defaultValue;
// }
return doubleValue;
}
代码示例来源:origin: b3log/latke
/**
* Get the optional float value associated with an index. The defaultValue
* is returned if there is no value for the index, or if the value is not a
* number and cannot be converted to a number.
*
* @param index
* subscript
* @param defaultValue
* The default value.
* @return The value.
*/
public float optFloat(int index, float defaultValue) {
final Number val = this.optNumber(index, null);
if (val == null) {
return defaultValue;
}
final float floatValue = val.floatValue();
// if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) {
// return floatValue;
// }
return floatValue;
}
代码示例来源:origin: org.b3log/latke
/**
* Get an optional {@link Number} value associated with a key, or null
* 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.
* @return An object which is the value.
*/
public Number optNumber(int index) {
return this.optNumber(index, null);
}
代码示例来源:origin: org.b3log/latke
/**
* Get the optional int value associated with an index. The defaultValue is
* returned if there is no value for the index, or if the value is not a
* number and cannot be converted to a number.
*
* @param index
* The index must be between 0 and length() - 1.
* @param defaultValue
* The default value.
* @return The value.
*/
public int optInt(int index, int defaultValue) {
final Number val = this.optNumber(index, null);
if (val == null) {
return defaultValue;
}
return val.intValue();
}
代码示例来源:origin: org.b3log/latke
/**
* Get the optional long value associated with an index. The defaultValue is
* returned if there is no value for the index, or if the value is not a
* number and cannot be converted to a number.
*
* @param index
* The index must be between 0 and length() - 1.
* @param defaultValue
* The default value.
* @return The value.
*/
public long optLong(int index, long defaultValue) {
final Number val = this.optNumber(index, null);
if (val == null) {
return defaultValue;
}
return val.longValue();
}
代码示例来源:origin: org.b3log/latke
/**
* Get the optional double value associated with an index. The defaultValue
* is returned if there is no value for the index, or if the value is not a
* number and cannot be converted to a number.
*
* @param index
* subscript
* @param defaultValue
* The default value.
* @return The value.
*/
public double optDouble(int index, double defaultValue) {
final Number val = this.optNumber(index, null);
if (val == null) {
return defaultValue;
}
final double doubleValue = val.doubleValue();
// if (Double.isNaN(doubleValue) || Double.isInfinite(doubleValue)) {
// return defaultValue;
// }
return doubleValue;
}
代码示例来源:origin: org.b3log/latke
/**
* Get the optional float value associated with an index. The defaultValue
* is returned if there is no value for the index, or if the value is not a
* number and cannot be converted to a number.
*
* @param index
* subscript
* @param defaultValue
* The default value.
* @return The value.
*/
public float optFloat(int index, float defaultValue) {
final Number val = this.optNumber(index, null);
if (val == null) {
return defaultValue;
}
final float floatValue = val.floatValue();
// if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) {
// return floatValue;
// }
return floatValue;
}
代码示例来源:origin: jnidzwetzki/bitfinex-v2-wss-api-java
private BitfinexExecutedTrade jsonToExecutedTrade(final JSONArray jsonArray) {
final BitfinexExecutedTrade executedTrade = new BitfinexExecutedTrade();
final long id = jsonArray.getNumber(0).longValue();
executedTrade.setTradeId(id);
final long timestamp = jsonArray.getNumber(1).longValue();
executedTrade.setTimestamp(timestamp);
final BigDecimal amount = jsonArray.getBigDecimal(2);
executedTrade.setAmount(amount);
// Funding or Currency
if (jsonArray.optNumber(4) != null) {
final BigDecimal rate = jsonArray.getBigDecimal(3);
executedTrade.setRate(rate);
final Long period = jsonArray.getLong(4);
executedTrade.setPeriod(period);
} else {
final BigDecimal price = jsonArray.getBigDecimal(3);
executedTrade.setPrice(price);
}
return executedTrade;
}