本文整理了Java中org.apache.sling.commons.json.JSONObject.opt()
方法的一些代码示例,展示了JSONObject.opt()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JSONObject.opt()
方法的具体详情如下:
包路径:org.apache.sling.commons.json.JSONObject
类名称:JSONObject
方法名:opt
[英]Get an optional value associated with a key.
[中]获取与键关联的可选值。
代码示例来源:origin: org.apache.sling/org.apache.sling.commons.json
/**
* Get an optional JSONObject associated with a key.
* It returns null if there is no such key, or if its value is not a
* JSONObject.
*
* @param key A key string.
* @return A JSONObject which is the value.
*/
public JSONObject optJSONObject(String key) {
Object o = opt(key);
return o instanceof JSONObject ? (JSONObject)o : null;
}
代码示例来源:origin: org.apache.sling/org.apache.sling.commons.json
/**
* Append a JSON Object
*
* @param o
* @return
* @throws JSONException
*/
public JSONWriter writeObject(JSONObject o) throws JSONException {
Iterator<String> keys = o.keys();
this.object();
while (keys.hasNext()) {
String key = keys.next();
this.key(key);
JSONObject objVal = o.optJSONObject(key);
if (objVal != null) {
this.writeObject(objVal);
continue;
}
JSONArray arrVal = o.optJSONArray(key);
if (arrVal != null) {
this.writeArray(arrVal);
continue;
}
Object obj = o.opt(key);
this.value(obj);
}
this.endObject();
return this;
}
代码示例来源:origin: io.wcm/io.wcm.handler.richtext
Object value = metadata.opt(metadataPropertyName);
if (value != null) {
resourceProps.put(metadataPropertyName, value);
代码示例来源:origin: org.apache.sling/org.apache.sling.commons.json
/**
* Get an optional JSONArray associated with a key.
* It returns null if there is no such key, or if its value is not a
* JSONArray.
*
* @param key A key string.
* @return A JSONArray which is the value.
*/
public JSONArray optJSONArray(String key) {
Object o = opt(key);
return o instanceof JSONArray ? (JSONArray)o : null;
}
代码示例来源:origin: org.apache.sling/org.apache.sling.commons.json
/**
* Determine if the value associated with the key is null or if there is
* no value.
* @param key A key string.
* @return true if there is no value associated with the key or if
* the value is the JSONObject.NULL object.
*/
public boolean isNull(String key) {
return JSONObject.NULL.equals(opt(key));
}
代码示例来源:origin: org.apache.sling/org.apache.sling.commons.json
/**
* Get an optional string associated with a key.
* It returns the defaultValue if there is no such key.
*
* @param key A key string.
* @param defaultValue The default.
* @return A string which is the value.
*/
public String optString(String key, String defaultValue) {
Object o = opt(key);
return o != null ? o.toString() : defaultValue;
}
代码示例来源:origin: org.apache.sling/org.apache.sling.commons.json
/**
* 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) {
try {
Object o = opt(key);
return o instanceof Number ? ((Number)o).doubleValue() :
new Double((String)o).doubleValue();
} catch (Exception e) {
return defaultValue;
}
}
代码示例来源:origin: org.apache.sling/org.apache.sling.commons.json
/**
* Construct a JSONObject from a subset of another JSONObject.
* An array of strings is used to identify the keys that should be copied.
* Missing keys are ignored.
* @param jo A JSONObject.
* @param sa An array of strings.
* @exception JSONException If a value is a non-finite number.
*/
public JSONObject(JSONObject jo, String[] sa) throws JSONException {
this(); // basic setup
for (int i = 0; i < sa.length; i += 1) {
putOpt(sa[i], jo.opt(sa[i]));
}
}
代码示例来源:origin: org.apache.sling/org.apache.sling.commons.json
/** Render the supplied JSONObject to a String, in
* the simplest possible way.
*/
public String toString(JSONObject jo) {
try {
final Iterator<String> keys = jo.keys();
final StringBuffer sb = new StringBuffer("{");
while (keys.hasNext()) {
if (sb.length() > 1) {
sb.append(',');
}
String o = keys.next();
sb.append(quote(o));
sb.append(':');
sb.append(valueToString(jo.opt(o)));
}
sb.append('}');
return sb.toString();
} catch (Exception e) {
return null;
}
}
代码示例来源:origin: org.apache.sling/org.apache.sling.commons.json
/**
* 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 {
Object o = opt(key);
if (o == null) {
throw new JSONException("JSONObject[" + quote(key) +
"] not found.");
}
return o;
}
代码示例来源:origin: io.wcm/io.wcm.testing.sling-mock
Object primaryTypeObj = jsonObject.opt(JcrConstants.JCR_PRIMARYTYPE);
String primaryType = null;
if (primaryTypeObj != null) {
代码示例来源:origin: org.apache.sling/org.apache.sling.commons.json
/**
* Produce a JSONArray containing the values of the members of this
* JSONObject.
* @param names A JSONArray containing a list of key strings. This
* determines the sequence of the values in the result.
* @return A JSONArray of values.
* @throws JSONException If any of the values are non-finite numbers.
*/
public JSONArray toJSONArray(JSONArray names) throws JSONException {
if (names == null || names.length() == 0) {
return null;
}
JSONArray ja = new JSONArray();
for (int i = 0; i < names.length(); i += 1) {
ja.put(this.opt(names.getString(i)));
}
return ja;
}
代码示例来源:origin: io.wcm/io.wcm.handler.richtext
/**
* Support legacy data structures where link metadata is stored as JSON fragment in single HTML5 data attribute.
* @param resourceProps ValueMap to write link metadata to
* @param element Link element
*/
private boolean getAnchorLegacyMetadataFromSingleData(ValueMap resourceProps, Element element) {
boolean foundAny = false;
JSONObject metadata = null;
Attribute dataAttribute = element.getAttribute("data");
if (dataAttribute != null) {
String metadataString = dataAttribute.getValue();
if (StringUtils.isNotEmpty(metadataString)) {
try {
metadata = new JSONObject(metadataString);
}
catch (JSONException ex) {
log.debug("Invalid link metadata: " + metadataString, ex);
}
}
}
if (metadata != null) {
JSONArray names = metadata.names();
for (int i = 0; i < names.length(); i++) {
String name = names.optString(i);
resourceProps.put(name, metadata.opt(name));
foundAny = true;
}
}
return foundAny;
}
代码示例来源:origin: org.apache.sling/org.apache.sling.commons.json
/**
* Accumulate values under a key. It is similar to the put method except
* that if there is already an object stored under the key then a
* JSONArray is stored under the key to hold all of the accumulated values.
* If there is already a JSONArray, then the new value is appended to it.
* In contrast, the put method replaces the previous value.
* @param key A key string.
* @param value An object to be accumulated under the key.
* @return this.
* @throws JSONException If the value is an invalid number
* or if the key is null.
*/
public JSONObject accumulate(String key, Object value)
throws JSONException {
testValidity(value);
Object o = opt(key);
if (o == null) {
put(key, value);
} else if (o instanceof JSONArray) {
((JSONArray)o).put(value);
} else {
put(key, new JSONArray().put(o).put(value));
}
return this;
}
代码示例来源:origin: org.apache.sling/org.apache.sling.commons.json
/**
* Append values to the array under a key. If the key does not exist in the
* JSONObject, then the key is put in the JSONObject with its value being a
* JSONArray containing the value parameter. If the key was already
* associated with a JSONArray, then the value parameter is appended to it.
* @param key A key string.
* @param value An object to be accumulated under the key.
* @return this.
* @throws JSONException If the key is null or if the current value
* associated with the key is not a JSONArray.
*/
public JSONObject append(String key, Object value)
throws JSONException {
testValidity(value);
Object o = opt(key);
if (o == null) {
put(key, new JSONArray().put(value));
} else if (o instanceof JSONArray) {
put(key, new JSONArray().put(o).put(value));
} else {
throw new JSONException("JSONObject[" + key +
"] is not a JSONArray.");
}
return this;
}
代码示例来源:origin: org.apache.sling/org.apache.sling.commons.json
context.accumulate(n, "");
} else if (o.length() == 1 &&
o.opt("content") != null) {
context.accumulate(n, o.opt("content"));
} else {
context.accumulate(n, o);
内容来源于网络,如有侵权,请联系作者删除!