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

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

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

JSONObject.quote介绍

[英]Encodes data as a JSON string. This applies quotes and any necessary character escaping.
[中]将数据编码为JSON字符串。这将应用引号和任何必要的字符转义。

代码示例

代码示例来源:origin: apache/geode

public static String quote(String string) {
 return JSONObject.quote(string);
}

代码示例来源:origin: zzz40500/GsonFormat

/**
 * Produce a string in double quotes with backslash sequences in all the
 * right places. A backslash will be inserted within </, producing <\/,
 * allowing JSON text to be delivered in HTML. In JSON text, a string cannot
 * contain a control character or an unescaped quote or backslash.
 *
 * @param string
 *            A String
 * @return A String correctly formatted for insertion in a JSON text.
 */
public static String quote(String string) {
  StringWriter sw = new StringWriter();
  synchronized (sw.getBuffer()) {
    try {
      return quote(string, sw).toString();
    } catch (IOException ignored) {
      // will never happen - we are writing to a string writer
      return "";
    }
  }
}

代码示例来源:origin: zzz40500/GsonFormat

/**
 * Get the JSONArray value associated with a key.
 *
 * @param key
 *            A key string.
 * @return A JSONArray which is the value.
 * @throws JSONException
 *             if the key is not found or if the value is not a JSONArray.
 */
public JSONArray getJSONArray(String key) throws JSONException {
  Object object = this.get(key);
  if (object instanceof JSONArray) {
    return (JSONArray) object;
  }
  throw new JSONException("JSONObject[" + quote(key)
      + "] is not a JSONArray.");
}

代码示例来源:origin: zzz40500/GsonFormat

/**
 * Get the string associated with a key.
 *
 * @param key
 *            A key string.
 * @return A string which is the value.
 * @throws JSONException
 *             if there is no string value for the key.
 */
public String getString(String key) throws JSONException {
  Object object = this.get(key);
  if (object instanceof String) {
    return (String) object;
  }
  throw new JSONException("JSONObject[" + quote(key) + "] not a string.");
}

代码示例来源:origin: zzz40500/GsonFormat

/**
 * Get the JSONObject value associated with a key.
 *
 * @param key
 *            A key string.
 * @return A JSONObject which is the value.
 * @throws JSONException
 *             if the key is not found or if the value is not a JSONObject.
 */
public JSONObject getJSONObject(String key) throws JSONException {
  Object object = this.get(key);
  if (object instanceof JSONObject) {
    return (JSONObject) object;
  }
  throw new JSONException("JSONObject[" + quote(key)
      + "] is not a JSONObject.");
}

代码示例来源:origin: zzz40500/GsonFormat

/**
 * Get the int value associated with a key.
 *
 * @param key
 *            A key string.
 * @return The integer value.
 * @throws JSONException
 *             if the key is not found or if the value cannot be converted
 *             to an integer.
 */
public int getInt(String key) throws JSONException {
  Object object = this.get(key);
  try {
    return object instanceof Number ? ((Number) object).intValue()
        : Integer.parseInt((String) object);
  } catch (Exception e) {
    throw new JSONException("JSONObject[" + quote(key)
        + "] is not an int.");
  }
}

代码示例来源:origin: zzz40500/GsonFormat

/**
 * Get the double 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 double getDouble(String key) throws JSONException {
  Object object = this.get(key);
  try {
    return object instanceof Number ? ((Number) object).doubleValue()
        : Double.parseDouble((String) object);
  } catch (Exception e) {
    throw new JSONException("JSONObject[" + quote(key)
        + "] is not a number.");
  }
}

代码示例来源:origin: zzz40500/GsonFormat

/**
 * Get the long value associated with a key.
 *
 * @param key
 *            A key string.
 * @return The long value.
 * @throws JSONException
 *             if the key is not found or if the value cannot be converted
 *             to a long.
 */
public long getLong(String key) throws JSONException {
  Object object = this.get(key);
  try {
    return object instanceof Number ? ((Number) object).longValue()
        : Long.parseLong((String) object);
  } catch (Exception e) {
    throw new JSONException("JSONObject[" + quote(key)
        + "] is not a long.");
  }
}

代码示例来源:origin: loklak/loklak_server

/**
 * Get the string associated with a key.
 *
 * @param key
 *            A key string.
 * @return A string which is the value.
 * @throws JSONException
 *             if there is no string value for the key.
 */
public String getString(String key) throws JSONException {
  Object object = this.get(key);
  if (object instanceof String) {
    return (String) object;
  }
  throw new JSONException("JSONObject[" + quote(key) + "] not a string.");
}

代码示例来源:origin: zzz40500/GsonFormat

/**
 * Get the boolean value associated with a key.
 *
 * @param key
 *            A key string.
 * @return The truth.
 * @throws JSONException
 *             if the value is not a Boolean or the String "true" or
 *             "false".
 */
public boolean getBoolean(String key) throws JSONException {
  Object object = this.get(key);
  if (object.equals(Boolean.FALSE)
      || (object instanceof String && ((String) object)
          .equalsIgnoreCase("false"))) {
    return false;
  } else if (object.equals(Boolean.TRUE)
      || (object instanceof String && ((String) object)
          .equalsIgnoreCase("true"))) {
    return true;
  }
  throw new JSONException("JSONObject[" + quote(key)
      + "] is not a Boolean.");
}

代码示例来源:origin: loklak/loklak_server

/**
 * Get the JSONArray value associated with a key.
 *
 * @param key
 *            A key string.
 * @return A JSONArray which is the value.
 * @throws JSONException
 *             if the key is not found or if the value is not a JSONArray.
 */
public JSONArray getJSONArray(String key) throws JSONException {
  Object object = this.get(key);
  if (object instanceof JSONArray) {
    return (JSONArray) object;
  }
  throw new JSONException("JSONObject[" + quote(key)
      + "] is not a JSONArray.");
}

代码示例来源:origin: loklak/loklak_server

/**
 * Get the JSONObject value associated with a key.
 *
 * @param key
 *            A key string.
 * @return A JSONObject which is the value.
 * @throws JSONException
 *             if the key is not found or if the value is not a JSONObject.
 */
public JSONObject getJSONObject(String key) throws JSONException {
  Object object = this.get(key);
  if (object instanceof JSONObject) {
    return (JSONObject) object;
  }
  throw new JSONException("JSONObject[" + quote(key)
      + "] is not a JSONObject.");
}

代码示例来源:origin: loklak/loklak_server

/**
 * Get the BigInteger 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 cannot 
 *             be converted to BigInteger.
 */
public BigInteger getBigInteger(String key) throws JSONException {
  Object object = this.get(key);
  try {
    return new BigInteger(object.toString());
  } catch (Exception e) {
    throw new JSONException("JSONObject[" + quote(key)
        + "] could not be converted to BigInteger.", e);
  }
}

代码示例来源:origin: loklak/loklak_server

/**
 * Get the double 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 double getDouble(String key) throws JSONException {
  Object object = this.get(key);
  try {
    return object instanceof Number ? ((Number) object).doubleValue()
        : Double.parseDouble(object.toString());
  } catch (Exception e) {
    throw new JSONException("JSONObject[" + quote(key)
        + "] is not a number.", e);
  }
}

代码示例来源:origin: loklak/loklak_server

/**
 * Get the float 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 float getFloat(String key) throws JSONException {
  Object object = this.get(key);
  try {
    return object instanceof Number ? ((Number) object).floatValue()
        : Float.parseFloat(object.toString());
  } catch (Exception e) {
    throw new JSONException("JSONObject[" + quote(key)
        + "] is not a number.", e);
  }
}

代码示例来源:origin: loklak/loklak_server

/**
 * Get the int value associated with a key.
 *
 * @param key
 *            A key string.
 * @return The integer value.
 * @throws JSONException
 *             if the key is not found or if the value cannot be converted
 *             to an integer.
 */
public int getInt(String key) throws JSONException {
  Object object = this.get(key);
  try {
    return object instanceof Number ? ((Number) object).intValue()
        : Integer.parseInt((String) object);
  } catch (Exception e) {
    throw new JSONException("JSONObject[" + quote(key)
        + "] is not an int.", e);
  }
}

代码示例来源:origin: loklak/loklak_server

/**
 * Get the long value associated with a key.
 *
 * @param key
 *            A key string.
 * @return The long value.
 * @throws JSONException
 *             if the key is not found or if the value cannot be converted
 *             to a long.
 */
public long getLong(String key) throws JSONException {
  Object object = this.get(key);
  try {
    return object instanceof Number ? ((Number) object).longValue()
        : Long.parseLong((String) object);
  } catch (Exception e) {
    throw new JSONException("JSONObject[" + quote(key)
        + "] is not a long.", e);
  }
}

代码示例来源:origin: zzz40500/GsonFormat

/**
 * Increment a property of a JSONObject. If there is no such property,
 * create one with a value of 1. If there is such a property, and if it is
 * an Integer, Long, Double, or Float, then add one to it.
 *
 * @param key
 *            A key string.
 * @return this.
 * @throws JSONException
 *             If there is already a property with this name that is not an
 *             Integer, Long, Double, or Float.
 */
public JSONObject increment(String key) throws JSONException {
  Object value = this.opt(key);
  if (value == null) {
    this.put(key, 1);
  } else if (value instanceof Integer) {
    this.put(key, (Integer) value + 1);
  } else if (value instanceof Long) {
    this.put(key, (Long) value + 1);
  } else if (value instanceof Double) {
    this.put(key, (Double) value + 1);
  } else if (value instanceof Float) {
    this.put(key, (Float) value + 1);
  } else {
    throw new JSONException("Unable to increment [" + quote(key) + "].");
  }
  return this;
}

代码示例来源:origin: zzz40500/GsonFormat

/**
 * Get the value object associated with a key.
 *
 * @param key
 *            A key string.
 * @return The object associated with the key.
 * @throws JSONException
 *             if the key is not found.
 */
public Object get(String key) throws JSONException {
  if (key == null) {
    throw new JSONException("Null key.");
  }
  Object object = this.opt(key);
  if (object == null) {
    throw new JSONException("JSONObject[" + quote(key) + "] not found.");
  }
  return object;
}

代码示例来源:origin: loklak/loklak_server

/**
 * Get the value object associated with a key.
 *
 * @param key
 *            A key string.
 * @return The object associated with the key.
 * @throws JSONException
 *             if the key is not found.
 */
public Object get(String key) throws JSONException {
  if (key == null) {
    throw new JSONException("Null key.");
  }
  Object object = this.opt(key);
  if (object == null) {
    throw new JSONException("JSONObject[" + quote(key) + "] not found.");
  }
  return object;
}

相关文章

微信公众号

最新文章

更多

JSONObject类方法