- Jsonarray class in java
- Methods inherited from class java.lang.Object
- Methods inherited from interface java.lang.Iterable
- Constructor Detail
- JSONArray
- JSONArray
- JSONArray
- JSONArray
- JSONArray
- JSONArray
- JSONArray
- JSONArray
- Method Detail
- iterator
- get
- getBoolean
- getDouble
- getFloat
- getNumber
- getEnum
- getBigDecimal
- getBigInteger
- getInt
- getJSONArray
- getJSONObject
- getLong
- getString
- isNull
- join
- length
- clear
- opt
- optBoolean
- optBoolean
- optDouble
- optDouble
- optFloat
- optFloat
- optInt
- optInt
- optEnum
- optEnum
- optBigInteger
- optBigDecimal
- optJSONArray
- optJSONObject
- optLong
- optLong
- optNumber
- optNumber
- optString
- optString
- put
- put
- put
- put
- put
- put
- put
- put
- put
- put
- put
- put
- put
- put
- put
- put
- putAll
- putAll
- putAll
- putAll
- query
- query
- optQuery
- optQuery
- remove
- similar
- toJSONObject
- toString
- toString
- write
- write
- toList
- isEmpty
- Jsonarray class in java
Jsonarray class in java
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.
Get an optional Number value associated with a key, or the default if there is no such key or if the value is not a number.
Queries and returns a value from this object using jsonPointer , or returns null if the query fails due to a missing key.
Queries and returns a value from this object using jsonPointer , or returns null if the query fails due to a missing key.
Put a value in the JSONArray, where the value will be a JSONArray which is produced from a Collection.
Put a value in the JSONArray, where the value will be a JSONArray which is produced from a Collection.
Creates a JSONPointer using an initialization string and tries to match it to an item within this JSONArray.
Methods inherited from class java.lang.Object
Methods inherited from interface java.lang.Iterable
Constructor Detail
JSONArray
JSONArray
JSONArray
JSONArray
public JSONArray(java.util.Collection collection)
JSONArray
public JSONArray(java.lang.Iterable iter)
JSONArray
JSONArray
JSONArray
Method Detail
iterator
public java.util.Iterator iterator()
get
getBoolean
Get the boolean value associated with an index. The string values «true» and «false» are converted to boolean.
getDouble
getFloat
getNumber
getEnum
getBigDecimal
Get the BigDecimal value associated with an index. If the value is float or double, the BigDecimal.BigDecimal(double) constructor will be used. See notes on the constructor for conversion issues that may arise.
getBigInteger
getInt
getJSONArray
public JSONArray getJSONArray(int index) throws JSONException
getJSONObject
public JSONObject getJSONObject(int index) throws JSONException
getLong
getString
isNull
public boolean isNull(int index)
join
Make a string from the contents of this JSONArray. The separator string is inserted between each element. Warning: This method assumes that the data structure is acyclical.
length
clear
Removes all of the elements from this JSONArray. The JSONArray will be empty after this call returns.
opt
public java.lang.Object opt(int index)
optBoolean
public boolean optBoolean(int index)
Get the optional boolean value associated with an index. It returns false if there is no value at that index, or if the value is not Boolean.TRUE or the String «true».
optBoolean
public boolean optBoolean(int index, boolean defaultValue)
Get the optional boolean value associated with an index. It returns the defaultValue if there is no value at that index or if it is not a Boolean or the String «true» or «false» (case insensitive).
optDouble
public double optDouble(int index)
Get the optional double value associated with an index. NaN 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.
optDouble
public double optDouble(int index, double defaultValue)
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.
optFloat
public float optFloat(int index)
Get the optional float value associated with an index. NaN 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.
optFloat
public float optFloat(int index, float defaultValue)
Get the optional float 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.
optInt
public int optInt(int index)
Get the optional int value associated with an index. Zero 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.
optInt
public int optInt(int index, int defaultValue)
Get the optional int 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.
optEnum
public > E optEnum(java.lang.Class clazz, int index)
optEnum
public > E optEnum(java.lang.Class clazz, int index, E defaultValue)
optBigInteger
public java.math.BigInteger optBigInteger(int index, java.math.BigInteger defaultValue)
Get the optional BigInteger 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.
optBigDecimal
public java.math.BigDecimal optBigDecimal(int index, java.math.BigDecimal defaultValue)
Get the optional BigDecimal 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. If the value is float or double, the BigDecimal.BigDecimal(double) constructor will be used. See notes on the constructor for conversion issues that may arise.
optJSONArray
optJSONObject
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 is not a JSONObject.
optLong
public long optLong(int index)
Get the optional long value associated with an index. Zero 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.
optLong
public long optLong(int index, long defaultValue)
Get the optional long 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.
optNumber
public java.lang.Number optNumber(int index)
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.
optNumber
public java.lang.Number optNumber(int index, java.lang.Number defaultValue)
Get an optional Number 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 ( BigDecimal ). This method would be used in cases where type coercion of the number value is unwanted.
optString
public java.lang.String optString(int index)
Get the optional string value associated with an index. It returns an empty string if there is no value at that index. If the value is not a string and is not null, then it is converted to a string.
optString
public java.lang.String optString(int index, java.lang.String defaultValue)
Get the optional string associated with an index. The defaultValue is returned if the key is not found.
put
put
public JSONArray put(java.util.Collection value)
Put a value in the JSONArray, where the value will be a JSONArray which is produced from a Collection.
put
public JSONArray put(double value) throws JSONException
put
public JSONArray put(float value) throws JSONException
put
put
put
put
put
public JSONArray put(int index, boolean value) throws JSONException
Put or replace a boolean value in the JSONArray. If the index is greater than the length of the JSONArray, then null elements will be added as necessary to pad it out.
put
public JSONArray put(int index, java.util.Collection value) throws JSONException
Put a value in the JSONArray, where the value will be a JSONArray which is produced from a Collection.
put
public JSONArray put(int index, double value) throws JSONException
Put or replace a double value. If the index is greater than the length of the JSONArray, then null elements will be added as necessary to pad it out.
put
public JSONArray put(int index, float value) throws JSONException
Put or replace a float value. If the index is greater than the length of the JSONArray, then null elements will be added as necessary to pad it out.
put
public JSONArray put(int index, int value) throws JSONException
Put or replace an int value. If the index is greater than the length of the JSONArray, then null elements will be added as necessary to pad it out.
put
public JSONArray put(int index, long value) throws JSONException
Put or replace a long value. If the index is greater than the length of the JSONArray, then null elements will be added as necessary to pad it out.
put
public JSONArray put(int index, java.util.Map value) throws JSONException
put
public JSONArray put(int index, java.lang.Object value) throws JSONException
Put or replace an object value in the JSONArray. If the index is greater than the length of the JSONArray, then null elements will be added as necessary to pad it out.
putAll
public JSONArray putAll(java.util.Collection collection)
putAll
public JSONArray putAll(java.lang.Iterable iter)
putAll
putAll
public JSONArray putAll(java.lang.Object array) throws JSONException
query
public java.lang.Object query(java.lang.String jsonPointer)
Creates a JSONPointer using an initialization string and tries to match it to an item within this JSONArray. For example, given a JSONArray initialized with this document:
Then this method will return the String «c» A JSONPointerException may be thrown from code called by this method.
query
Uses a user initialized JSONPointer and tries to match it to an item within this JSONArray. For example, given a JSONArray initialized with this document:
Then this method will return the String «c» A JSONPointerException may be thrown from code called by this method.
optQuery
public java.lang.Object optQuery(java.lang.String jsonPointer)
Queries and returns a value from this object using jsonPointer , or returns null if the query fails due to a missing key.
optQuery
Queries and returns a value from this object using jsonPointer , or returns null if the query fails due to a missing key.
remove
public java.lang.Object remove(int index)
similar
public boolean similar(java.lang.Object other)
toJSONObject
public JSONObject toJSONObject(JSONArray names) throws JSONException
toString
public java.lang.String toString()
Make a JSON text of this JSONArray. For compactness, no unnecessary whitespace is added. If it is not possible to produce a syntactically correct JSON text then null will be returned instead. This could occur if the array contains an invalid number. Warning: This method assumes that the data structure is acyclical.
toString
write
Write the contents of the JSONArray as JSON text to a writer. For compactness, no whitespace is added. Warning: This method assumes that the data structure is acyclical.
write
toList
public java.util.List toList()
Returns a java.util.List containing all of the elements in this array. If an element in the array is a JSONArray or JSONObject it will also be converted to a List and a Map respectively. Warning: This method assumes that the data structure is acyclical.
isEmpty
Jsonarray class in java
JsonArray represents an immutable JSON array (an ordered sequence of zero or more values). It also provides an unmodifiable list view of the values in the array. A JsonArray object can be created by reading JSON data from an input source or it can be built from scratch using an array builder object. The following example demonstrates how to create a JsonArray object from an input source using the method JsonReader.readArray() :
JsonReader jsonReader = Json.createReader(. ); JsonArray array = jsonReader.readArray(); jsonReader.close();
The following example demonstrates how to build an empty JSON array using the class JsonArrayBuilder :
JsonArray array = Json.createArrayBuilder().build();
JsonArray value = Json.createArrayBuilder() .add(Json.createObjectBuilder() .add("type", "home") .add("number", "212 555-1234")) .add(Json.createObjectBuilder() .add("type", "fax") .add("number", "646 555-4567")) .build();
JsonArray arr = . ; JsonWriter writer = Json.createWriter(. ) writer.writeArray(arr); writer.close();
The values in a JsonArray can be of the following types: JsonObject , JsonArray , JsonString , JsonNumber , JsonValue.TRUE , JsonValue.FALSE , and JsonValue.NULL . JsonArray provides various accessor methods to access the values in an array. The following example shows how to obtain the home phone number «212 555-1234» from the array built in the previous example:
JsonObject home = array.getJsonObject(0); String number = home.getString("number");
JsonArray instances are list objects that provide read-only access to the values in the JSON array. Any attempt to modify the list, whether directly or using its collection views, results in an UnsupportedOperationException .