java.lang.Object
at.syntaxerror.json5.JSONObject
A JSONObject is a map (key-value) structure capable of holding multiple values,
including other
JSONArrays and JSONObjects- Author:
- SyntaxError404
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new JSONObjectJSONObject(JSONParser parser) Constructs a new JSONObject from a JSONParserJSONObject(String source) Constructs a new JSONObject from a string -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Removes all values from this JSONObjectentrySet()Returns a set of entries of the JSONObject.Returns the value for a given keyReturns the value for a given key, or the default value if the operation is not possibleReturns the value as a JSONArray for a given keyReturns the value as a JSONArray for a given key, or the default value if the operation is not possiblebooleangetBoolean(String key) Returns the value as a boolean for a given keybooleangetBoolean(String key, boolean defaults) Returns the value as a boolean for a given key, or the default value if the operation is not possiblebyteReturns the value as a byte for a given keybyteReturns the value as a byte for a given key, or the default value if the operation is not possiblebytegetByteExact(String key) Returns the exact value as a byte for a given key.bytegetByteExact(String key, byte defaults) Returns the exact value as a byte for a given key, or the default value if the operation is not possibledoubleReturns the value as a double for a given keydoubleReturns the value as a double for a given key, or the default value if the operation is not possibledoublegetDoubleExact(String key) Returns the exact value as a double for a given key.doublegetDoubleExact(String key, double defaults) Returns the exact value as a double for a given key, or the default value if the operation is not possiblefloatReturns the value as a float for a given keyfloatReturns the value as a float for a given key, or the default value if the operation is not possiblefloatgetFloatExact(String key) Returns the exact value as a float for a given key.floatgetFloatExact(String key, float defaults) Returns the exact value as a float for a given key, or the default value if the operation is not possiblegetInstant(String key) Returns the value as an Instant for a given keygetInstant(String key, Instant defaults) Returns the value as an Instant for a given key, or the default value if the operation is not possibleintReturns the value as an int for a given keyintReturns the value as an int for a given key, or the default value if the operation is not possibleintgetIntExact(String key) Returns the exact value as an int for a given key.intgetIntExact(String key, int defaults) Returns the exact value as an int for a given key, or the default value if the operation is not possiblelongReturns the value as a long for a given keylongReturns the value as a long for a given key, or the default value if the operation is not possiblelonggetLongExact(String key) Returns the exact value as a long for a given key.longgetLongExact(String key, long defaults) Returns the exact value as a long for a given key, or the default value if the operation is not possibleReturns the value as a number for a given keyReturns the value as a number for a given key, or the default value if the operation is not possibleReturns the value as a JSONObject for a given keygetObject(String key, JSONObject defaults) Returns the value as a JSONObject for a given key, or the default value if the operation is not possibleshortReturns the value as a short for a given keyshortReturns the value as a short for a given key, or the default value if the operation is not possibleshortgetShortExact(String key) Returns the exact value as a short for a given key.shortgetShortExact(String key, short defaults) Returns the exact value as a short for a given key, or the default value if the operation is not possibleReturns the value as a string for a given keyReturns the value as a string for a given key, or the default value if the operation is not possiblebooleanChecks if a key exists within the JSONObjectbooleanChecks if the value with the specified key is a JSONArraybooleanChecks if the value with the specified key is a booleanbooleanChecks if the value with the specified key is an InstantbooleanChecks if the value with the specified key isnullbooleanChecks if the value with the specified key is a numberbooleanChecks if the value with the specified key is a JSONObjectbooleanChecks if the value with the specified key is a stringiterator()keySet()Returns a set of keys of the JSONObjectintlength()Returns the number of entries in the JSONObjectvoidRemoves a key from a JSONObjectSets the value at a given keytoMap()Converts the JSONObject into a map.toString()Converts the JSONObject into its compact string representation.toString(int indentFactor) Converts the JSONObject into its string representation.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
JSONObject
public JSONObject()Constructs a new JSONObject -
JSONObject
Constructs a new JSONObject from a string- Parameters:
source- a string
-
JSONObject
Constructs a new JSONObject from a JSONParser- Parameters:
parser- a JSONParser
-
-
Method Details
-
toMap
Converts the JSONObject into a map. All JSONObjects and JSONArrays contained within this JSONObject will be converted into their Map or List form as well- Returns:
- a map of the entries of this object
-
keySet
Returns a set of keys of the JSONObject- Returns:
- a set of keys
- See Also:
-
entrySet
Returns a set of entries of the JSONObject. Modifying the set or an entry will modify the JSONObject Use with caution.- Returns:
- a set of entries
- See Also:
-
iterator
-
length
public int length()Returns the number of entries in the JSONObject- Returns:
- the number of entries
-
clear
public void clear()Removes all values from this JSONObject- Since:
- 1.2.0
-
remove
Removes a key from a JSONObject- Parameters:
key- the key to be removed- Throws:
JSONException- if the key does not exist- Since:
- 1.2.0
-
has
Checks if a key exists within the JSONObject- Parameters:
key- the key- Returns:
- whether the key exists
-
isNull
Checks if the value with the specified key isnull- Parameters:
key- the key- Returns:
- whether the value is
null - Throws:
JSONException- if the key does not exist
-
isBoolean
Checks if the value with the specified key is a boolean- Parameters:
key- the key- Returns:
- whether the value is a boolean
- Throws:
JSONException- if the key does not exist
-
isString
Checks if the value with the specified key is a string- Parameters:
key- the key- Returns:
- whether the value is a string
- Throws:
JSONException- if the key does not exist
-
isNumber
Checks if the value with the specified key is a number- Parameters:
key- the key- Returns:
- whether the value is a number
- Throws:
JSONException- if the key does not exist
-
isObject
Checks if the value with the specified key is a JSONObject- Parameters:
key- the key- Returns:
- whether the value is a JSONObject
- Throws:
JSONException- if the key does not exist
-
isArray
Checks if the value with the specified key is a JSONArray- Parameters:
key- the key- Returns:
- whether the value is a JSONArray
- Throws:
JSONException- if the key does not exist
-
isInstant
Checks if the value with the specified key is an Instant- Parameters:
key- the key- Returns:
- whether the value is an Instant
- Throws:
JSONException- if the key does not exist- Since:
- 1.1.0
-
get
Returns the value for a given key- Parameters:
key- the key- Returns:
- the value
- Throws:
JSONException- if the key does not exist
-
getBoolean
Returns the value as a boolean for a given key- Parameters:
key- the key- Returns:
- the boolean
- Throws:
JSONException- if the key does not exist, or if the value is not a boolean
-
getString
Returns the value as a string for a given key- Parameters:
key- the key- Returns:
- the string
- Throws:
JSONException- if the key does not exist, or if the value is not a string
-
getNumber
Returns the value as a number for a given key- Parameters:
key- the key- Returns:
- the number
- Throws:
JSONException- if the key does not exist, or if the value is not a number
-
getByte
Returns the value as a byte for a given key- Parameters:
key- the key- Returns:
- the byte
- Throws:
JSONException- if the key does not exist, or if the value is not a byte
-
getShort
Returns the value as a short for a given key- Parameters:
key- the key- Returns:
- the short
- Throws:
JSONException- if the key does not exist, or if the value is not a short
-
getInt
Returns the value as an int for a given key- Parameters:
key- the key- Returns:
- the int
- Throws:
JSONException- if the key does not exist, or if the value is not an int
-
getLong
Returns the value as a long for a given key- Parameters:
key- the key- Returns:
- the long
- Throws:
JSONException- if the key does not exist, or if the value is not a long
-
getFloat
Returns the value as a float for a given key- Parameters:
key- the key- Returns:
- the float
- Throws:
JSONException- if the key does not exist, or if the value is not a float
-
getDouble
Returns the value as a double for a given key- Parameters:
key- the key- Returns:
- the double
- Throws:
JSONException- if the key does not exist, or if the value is not a double
-
getByteExact
Returns the exact value as a byte for a given key. This fails if the value does not fit into a byte- Parameters:
key- the key- Returns:
- the byte
- Throws:
JSONException- if the key does not exist, the value is not a byte, or if the value does not fit into a byte
-
getShortExact
Returns the exact value as a short for a given key. This fails if the value does not fit into a short- Parameters:
key- the key- Returns:
- the short
- Throws:
JSONException- if the key does not exist, the value is not a short, or if the value does not fit into a short
-
getIntExact
Returns the exact value as an int for a given key. This fails if the value does not fit into an int- Parameters:
key- the key- Returns:
- the int
- Throws:
JSONException- if the key does not exist, the value is not an int, or if the value does not fit into an int
-
getLongExact
Returns the exact value as a long for a given key. This fails if the value does not fit into a long- Parameters:
key- the key- Returns:
- the long
- Throws:
JSONException- if the key does not exist, the value is not a long, or if the value does not fit into a long
-
getFloatExact
Returns the exact value as a float for a given key. This fails if the value does not fit into a float- Parameters:
key- the key- Returns:
- the float
- Throws:
JSONException- if the key does not exist, the value is not a float, or if the value does not fit into a float
-
getDoubleExact
Returns the exact value as a double for a given key. This fails if the value does not fit into a double- Parameters:
key- the key- Returns:
- the double
- Throws:
JSONException- if the key does not exist, the value is not a double, or if the value does not fit into a double
-
getObject
Returns the value as a JSONObject for a given key- Parameters:
key- the key- Returns:
- the JSONObject
- Throws:
JSONException- if the key does not exist, or if the value is not a JSONObject
-
getArray
Returns the value as a JSONArray for a given key- Parameters:
key- the key- Returns:
- the JSONArray
- Throws:
JSONException- if the key does not exist, or if the value is not a JSONArray
-
getInstant
Returns the value as an Instant for a given key- Parameters:
key- the key- Returns:
- the Instant
- Throws:
JSONException- if the key does not exist, or if the value is not an Instant- Since:
- 1.1.0
-
get
Returns the value for a given key, or the default value if the operation is not possible- Parameters:
key- the keydefaults- the default value- Returns:
- the value
-
getBoolean
Returns the value as a boolean for a given key, or the default value if the operation is not possible- Parameters:
key- the keydefaults- the default value- Returns:
- the boolean
-
getString
Returns the value as a string for a given key, or the default value if the operation is not possible- Parameters:
key- the keydefaults- the default value- Returns:
- the string
-
getNumber
Returns the value as a number for a given key, or the default value if the operation is not possible- Parameters:
key- the keydefaults- the default value- Returns:
- the number
-
getByte
Returns the value as a byte for a given key, or the default value if the operation is not possible- Parameters:
key- the keydefaults- the default value- Returns:
- the byte
-
getShort
Returns the value as a short for a given key, or the default value if the operation is not possible- Parameters:
key- the keydefaults- the default value- Returns:
- the short
-
getInt
Returns the value as an int for a given key, or the default value if the operation is not possible- Parameters:
key- the keydefaults- the default value- Returns:
- the int
-
getLong
Returns the value as a long for a given key, or the default value if the operation is not possible- Parameters:
key- the keydefaults- the default value- Returns:
- the long
-
getFloat
Returns the value as a float for a given key, or the default value if the operation is not possible- Parameters:
key- the keydefaults- the default value- Returns:
- the float
-
getDouble
Returns the value as a double for a given key, or the default value if the operation is not possible- Parameters:
key- the keydefaults- the default value- Returns:
- the double
-
getByteExact
Returns the exact value as a byte for a given key, or the default value if the operation is not possible- Parameters:
key- the keydefaults- the default value- Returns:
- the byte
-
getShortExact
Returns the exact value as a short for a given key, or the default value if the operation is not possible- Parameters:
key- the keydefaults- the default value- Returns:
- the short
-
getIntExact
Returns the exact value as an int for a given key, or the default value if the operation is not possible- Parameters:
key- the keydefaults- the default value- Returns:
- the int
-
getLongExact
Returns the exact value as a long for a given key, or the default value if the operation is not possible- Parameters:
key- the keydefaults- the default value- Returns:
- the long
-
getFloatExact
Returns the exact value as a float for a given key, or the default value if the operation is not possible- Parameters:
key- the keydefaults- the default value- Returns:
- the float
-
getDoubleExact
Returns the exact value as a double for a given key, or the default value if the operation is not possible- Parameters:
key- the keydefaults- the default value- Returns:
- the double
-
getObject
Returns the value as a JSONObject for a given key, or the default value if the operation is not possible- Parameters:
key- the keydefaults- the default value- Returns:
- the JSONObject
-
getArray
Returns the value as a JSONArray for a given key, or the default value if the operation is not possible- Parameters:
key- the keydefaults- the default value- Returns:
- the JSONArray
-
getInstant
Returns the value as an Instant for a given key, or the default value if the operation is not possible- Parameters:
key- the keydefaults- the default value- Returns:
- the Instant
- Since:
- 1.1.0
-
set
Sets the value at a given key- Parameters:
key- the keyvalue- the new value- Returns:
- this JSONObject
-
toString
Converts the JSONObject into its string representation. The indentation factor enables pretty-printing and defines how many spaces (' ') should be placed before each key/value pair. A factor of< 1disables pretty-printing and discards any optional whitespace characters.indentFactor = 2:{ "key0": "value0", "key1": { "nested": 123 }, "key2": false }indentFactor = 0:{"key0":"value0","key1":{"nested":123},"key2":false}- Parameters:
indentFactor- the indentation factor- Returns:
- the string representation
- See Also:
-
toString
Converts the JSONObject into its compact string representation.
-