mirror of
https://github.com/stleary/JSON-java.git
synced 2025-08-03 03:15:32 -04:00
Add optJSONArray and optJSONObject methods to JSONArray with a default value
This commit is contained in:
parent
db0fde2a56
commit
284a316838
@ -924,30 +924,57 @@ public class JSONArray implements Iterable<Object> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the optional JSONArray associated with an index.
|
* Get the optional JSONArray associated with an index. Null is returned if
|
||||||
|
* there is no value at that index or if the value is not a JSONArray.
|
||||||
*
|
*
|
||||||
* @param index
|
* @param index
|
||||||
* subscript
|
* The index must be between 0 and length() - 1.
|
||||||
* @return A JSONArray value, or null if the index has no value, or if the
|
* @return A JSONArray value.
|
||||||
* value is not a JSONArray.
|
|
||||||
*/
|
*/
|
||||||
public JSONArray optJSONArray(int index) {
|
public JSONArray optJSONArray(int index) {
|
||||||
Object o = this.opt(index);
|
return this.optJSONArray(index, null);
|
||||||
return o instanceof JSONArray ? (JSONArray) o : null;
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the optional JSONArray associated with an index. The defaultValue is returned if
|
||||||
|
* there is no value at that index or if the value is not a JSONArray.
|
||||||
|
*
|
||||||
|
* @param index
|
||||||
|
* The index must be between 0 and length() - 1.
|
||||||
|
* @param defaultValue
|
||||||
|
* The default.
|
||||||
|
* @return A JSONArray value.
|
||||||
|
*/
|
||||||
|
public JSONArray optJSONArray(int index, JSONArray defaultValue) {
|
||||||
|
Object object = this.opt(index);
|
||||||
|
return object instanceof JSONArray ? (JSONArray) object : defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the optional JSONObject associated with an index. Null is returned if
|
* Get the optional JSONObject associated with an index. Null is returned if
|
||||||
* the key is not found, or null if the index has no value, or if the value
|
* there is no value at that index or if the value is not a JSONObject.
|
||||||
* is not a JSONObject.
|
|
||||||
*
|
*
|
||||||
* @param index
|
* @param index
|
||||||
* The index must be between 0 and length() - 1.
|
* The index must be between 0 and length() - 1.
|
||||||
* @return A JSONObject value.
|
* @return A JSONObject value.
|
||||||
*/
|
*/
|
||||||
public JSONObject optJSONObject(int index) {
|
public JSONObject optJSONObject(int index) {
|
||||||
Object o = this.opt(index);
|
return this.optJSONObject(index, null);
|
||||||
return o instanceof JSONObject ? (JSONObject) o : null;
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the optional JSONObject associated with an index. The defaultValue is returned if
|
||||||
|
* there is no value at that index or if the value is not a JSONObject.
|
||||||
|
*
|
||||||
|
* @param index
|
||||||
|
* The index must be between 0 and length() - 1.
|
||||||
|
* @param defaultValue
|
||||||
|
* The default.
|
||||||
|
* @return A JSONObject value.
|
||||||
|
*/
|
||||||
|
public JSONObject optJSONObject(int index, JSONObject defaultValue) {
|
||||||
|
Object object = this.opt(index);
|
||||||
|
return object instanceof JSONObject ? (JSONObject) object : defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -595,13 +595,17 @@ public class JSONArrayTest {
|
|||||||
|
|
||||||
JSONArray nestedJsonArray = jsonArray.optJSONArray(9);
|
JSONArray nestedJsonArray = jsonArray.optJSONArray(9);
|
||||||
assertTrue("Array opt JSONArray", nestedJsonArray != null);
|
assertTrue("Array opt JSONArray", nestedJsonArray != null);
|
||||||
assertTrue("Array opt JSONArray default",
|
assertTrue("Array opt JSONArray null",
|
||||||
null == jsonArray.optJSONArray(99));
|
null == jsonArray.optJSONArray(99));
|
||||||
|
assertTrue("Array opt JSONArray default",
|
||||||
|
"value".equals(jsonArray.optJSONArray(99, new JSONArray("[\"value\"]")).getString(0)));
|
||||||
|
|
||||||
JSONObject nestedJsonObject = jsonArray.optJSONObject(10);
|
JSONObject nestedJsonObject = jsonArray.optJSONObject(10);
|
||||||
assertTrue("Array opt JSONObject", nestedJsonObject != null);
|
assertTrue("Array opt JSONObject", nestedJsonObject != null);
|
||||||
assertTrue("Array opt JSONObject default",
|
assertTrue("Array opt JSONObject null",
|
||||||
null == jsonArray.optJSONObject(99));
|
null == jsonArray.optJSONObject(99));
|
||||||
|
assertTrue("Array opt JSONObject default",
|
||||||
|
"value".equals(jsonArray.optJSONObject(99, new JSONObject("{\"key\":\"value\"}")).getString("key")));
|
||||||
|
|
||||||
assertTrue("Array opt long",
|
assertTrue("Array opt long",
|
||||||
0 == jsonArray.optLong(11));
|
0 == jsonArray.optLong(11));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user