com.github.openjson.JSONObject.opt()方法的使用及代码示例

x33g5p2x  于2022-01-22 转载在 其他  
字(6.5k)|赞(0)|评价(0)|浏览(136)

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

JSONObject.opt介绍

[英]Returns the value mapped by name, or null if no such mapping exists.
[中]返回按名称映射的值,如果不存在此类映射,则返回null。

代码示例

代码示例来源:origin: com.github.openjson/openjson

/**
 * Returns the value mapped by {@code name} if it exists and is a {@code
 * JSONArray}, or null otherwise.
 *
 * @param name The name of the field we want.
 * @return The value of the specified field (assuming it is a JSNOArray
 */
public JSONArray optJSONArray(String name) {
  Object object = opt(name);
  return object instanceof JSONArray ? (JSONArray) object : null;
}

代码示例来源:origin: com.googlecode.wicket-jquery-ui/wicket-kendo-ui

@Override
public SchedulerEvent toObject(JSONObject object, List<ResourceList> lists)
{
  SchedulerEvent event = this.newSchedulerEvent();
  event.setId(object.get("id")); // Object
  event.setTitle(object.optString("title"));
  event.setDescription(object.optString("description"));
  event.setStart(DateUtils.toZonedDateTime(object.getLong("start"), this.offset));
  event.setUntil(DateUtils.toZonedDateTime(object.getLong("end"), this.offset));
  event.setAllDay(object.getBoolean("isAllDay"));
  event.setRecurrenceId(object.optString("recurrenceId"));
  event.setRecurrenceRule(object.optString("recurrenceRule"));
  event.setRecurrenceException(object.optString("recurrenceException"));
  // Resources //
  for (ResourceList list : lists)
  {
    String field = list.getField();
    Object value = object.opt(field);
    if (list.isMultiple() && value instanceof JSONArray)
    {
      event.setValue(field, JsonUtils.toList((JSONArray) value));
    }
    else
    {
      event.setValue(field, value);
    }
  }
  return event;
}

代码示例来源:origin: sebfz1/wicket-jquery-ui

@Override
public SchedulerEvent toObject(JSONObject object, List<ResourceList> lists)
{
  SchedulerEvent event = this.newSchedulerEvent();
  event.setId(object.get("id")); // Object
  event.setTitle(object.optString("title"));
  event.setDescription(object.optString("description"));
  event.setStart(DateUtils.toZonedDateTime(object.getLong("start"), this.offset));
  event.setUntil(DateUtils.toZonedDateTime(object.getLong("end"), this.offset));
  event.setAllDay(object.getBoolean("isAllDay"));
  event.setRecurrenceId(object.optString("recurrenceId"));
  event.setRecurrenceRule(object.optString("recurrenceRule"));
  event.setRecurrenceException(object.optString("recurrenceException"));
  // Resources //
  for (ResourceList list : lists)
  {
    String field = list.getField();
    Object value = object.opt(field);
    if (list.isMultiple() && value instanceof JSONArray)
    {
      event.setValue(field, JsonUtils.toList((JSONArray) value));
    }
    else
    {
      event.setValue(field, value);
    }
  }
  return event;
}

代码示例来源:origin: com.github.openjson/openjson

/**
 * Returns the value mapped by {@code name} if it exists and is a {@code
 * JSONObject}, or null otherwise.
 *
 * @param name The name of the value we want.
 * @return The specified value.
 */
public JSONObject optJSONObject(String name) {
  Object object = opt(name);
  return object instanceof JSONObject ? (JSONObject) object : null;
}

代码示例来源:origin: com.github.openjson/openjson

/**
 * Creates a new {@code JSONObject} by copying mappings for the listed names
 * from the given object. Names that aren't present in {@code copyFrom} will
 * be skipped.
 *
 * @param copyFrom The source object.
 * @param names    The names of the fields to copy.
 * @throws JSONException On internal errors. Shouldn't happen.
 */
public JSONObject(JSONObject copyFrom, String[] names) throws JSONException {
  this();
  for (String name : names) {
    Object value = copyFrom.opt(name);
    if (value != null) {
      nameValuePairs.put(name, value);
    }
  }
}

代码示例来源:origin: com.github.openjson/openjson

/**
 * Returns the value mapped by {@code name} if it exists and is a double or
 * can be coerced to a double, or {@code fallback} otherwise.
 *
 * @param name     The name of the field we want.
 * @param fallback The value to return if the field isn't there.
 * @return The selected value or the fallback.
 */
public double optDouble(String name, double fallback) {
  Object object = opt(name);
  Double result = JSON.toDouble(object);
  return result != null ? result : fallback;
}

代码示例来源:origin: com.github.openjson/openjson

/**
 * Returns the value mapped by {@code name} if it exists and is a boolean or
 * can be coerced to a boolean, or {@code fallback} otherwise.
 *
 * @param name     The name of the field we want.
 * @param fallback The value to return if the field isn't there.
 * @return The selected value or the fallback.
 */
public boolean optBoolean(String name, boolean fallback) {
  Object object = opt(name);
  Boolean result = JSON.toBoolean(object);
  return result != null ? result : fallback;
}

代码示例来源:origin: com.github.openjson/openjson

/**
 * Returns the value mapped by {@code name} if it exists, coercing it if
 * necessary, or {@code fallback} if no such mapping exists.
 *
 * @param name     The name of the field that we want.
 * @param fallback The value to return if the field doesn't exist.
 * @return The value of the field or fallback.
 */
public String optString(String name, String fallback) {
  Object object = opt(name);
  String result = JSON.toString(object);
  return result != null ? result : fallback;
}

代码示例来源:origin: com.github.openjson/openjson

/**
 * Returns the value mapped by {@code name} if it exists and is an int or
 * can be coerced to an int, or {@code fallback} otherwise.
 *
 * @param name     The name of the field we want.
 * @param fallback The value to return if the field isn't there.
 * @return The selected value or the fallback.
 */
public int optInt(String name, int fallback) {
  Object object = opt(name);
  Integer result = JSON.toInteger(object);
  return result != null ? result : fallback;
}

代码示例来源:origin: com.github.openjson/openjson

/**
 * Returns the value mapped by {@code name} if it exists and is a long or
 * can be coerced to a long, or {@code fallback} otherwise. Note that JSON represents
 * numbers as doubles, so this is <a href="#lossy">lossy</a>; use strings to transfer
 * numbers via JSON.
 *
 * @param name     The name of the field we want.
 * @param fallback The value to return if the field isn't there.
 * @return The selected value or the fallback.
 */
public long optLong(String name, long fallback) {
  Object object = opt(name);
  Long result = JSON.toLong(object);
  return result != null ? result : fallback;
}

代码示例来源:origin: com.github.openjson/openjson

/**
 * Returns an array with the values corresponding to {@code names}. The
 * array contains null for names that aren't mapped. This method returns
 * null if {@code names} is either null or empty.
 *
 * @param names The names of the fields that we want the values for.
 * @return The selected values.
 * @throws JSONException On internal errors. Shouldn't happen.
 */
public JSONArray toJSONArray(JSONArray names) throws JSONException {
  JSONArray result = new JSONArray();
  if (names == null) {
    return null;
  }
  int length = names.length();
  if (length == 0) {
    return null;
  }
  for (int i = 0; i < length; i++) {
    String name = JSON.toString(names.opt(i));
    result.put(opt(name));
  }
  return result;
}

相关文章