org.json.JSONObject.optNumber()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(5.7k)|赞(0)|评价(0)|浏览(167)

本文整理了Java中org.json.JSONObject.optNumber()方法的一些代码示例,展示了JSONObject.optNumber()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JSONObject.optNumber()方法的具体详情如下:
包路径:org.json.JSONObject
类名称:JSONObject
方法名:optNumber

JSONObject.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 <code>null</code>
 * 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 key
 *            A key string.
 * @return An object which is the value.
 */
public Number optNumber(String key) {
  return this.optNumber(key, null);
}

代码示例来源:origin: b3log/latke

/**
 * Get an optional {@link Number} value associated with a key, or <code>null</code>
 * 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 key
 *            A key string.
 * @return An object which is the value.
 */
public Number optNumber(String key) {
  return this.optNumber(key, null);
}

代码示例来源:origin: b3log/latke

/**
 * Get an optional long 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.
 *
 * @param key
 *            A key string.
 * @param defaultValue
 *            The default.
 * @return An object which is the value.
 */
public long optLong(String key, long defaultValue) {
  final Number val = this.optNumber(key, null);
  if (val == null) {
    return defaultValue;
  }
  
  return val.longValue();
}

代码示例来源:origin: b3log/latke

/**
 * Get an optional double associated with a key, or the defaultValue if
 * there is no such key or if its 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.
 * @param defaultValue
 *            The default.
 * @return An object which is the value.
 */
public double optDouble(String key, double defaultValue) {
  Number val = this.optNumber(key);
  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 an optional int 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.
 *
 * @param key
 *            A key string.
 * @param defaultValue
 *            The default.
 * @return An object which is the value.
 */
public int optInt(String key, int defaultValue) {
  final Number val = this.optNumber(key, null);
  if (val == null) {
    return defaultValue;
  }
  return val.intValue();
}

代码示例来源: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 key
 *            A key string.
 * @param defaultValue
 *            The default value.
 * @return The value.
 */
public float optFloat(String key, float defaultValue) {
  Number val = this.optNumber(key);
  if (val == null) {
    return defaultValue;
  }
  final float floatValue = val.floatValue();
  // if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) {
  // return defaultValue;
  // }
  return floatValue;
}

代码示例来源:origin: org.b3log/latke

/**
 * Get an optional {@link Number} value associated with a key, or <code>null</code>
 * 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 key
 *            A key string.
 * @return An object which is the value.
 */
public Number optNumber(String key) {
  return this.optNumber(key, null);
}

代码示例来源:origin: org.b3log/latke

/**
 * Get an optional int 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.
 *
 * @param key
 *            A key string.
 * @param defaultValue
 *            The default.
 * @return An object which is the value.
 */
public int optInt(String key, int defaultValue) {
  final Number val = this.optNumber(key, null);
  if (val == null) {
    return defaultValue;
  }
  return val.intValue();
}

代码示例来源:origin: org.b3log/latke

/**
 * Get an optional long 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.
 *
 * @param key
 *            A key string.
 * @param defaultValue
 *            The default.
 * @return An object which is the value.
 */
public long optLong(String key, long defaultValue) {
  final Number val = this.optNumber(key, null);
  if (val == null) {
    return defaultValue;
  }
  
  return val.longValue();
}

代码示例来源:origin: org.b3log/latke

/**
 * Get an optional double associated with a key, or the defaultValue if
 * there is no such key or if its 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.
 * @param defaultValue
 *            The default.
 * @return An object which is the value.
 */
public double optDouble(String key, double defaultValue) {
  Number val = this.optNumber(key);
  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 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 key
 *            A key string.
 * @param defaultValue
 *            The default value.
 * @return The value.
 */
public float optFloat(String key, float defaultValue) {
  Number val = this.optNumber(key);
  if (val == null) {
    return defaultValue;
  }
  final float floatValue = val.floatValue();
  // if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) {
  // return defaultValue;
  // }
  return floatValue;
}

相关文章

JSONObject类方法