- All Implemented Interfaces:
Serializable,Cloneable,Map<Object,Object>
Represents a JSON object.
- Since:
- 1.0.0
- Version:
- 2.1.0
- Author:
- FangYidong(fangyidong@yahoo.com.cn), Ralph Niemitz/RalleYTN(ralph.niemitz@gmx.de)
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K extends Object,V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object, V extends Object> -
Constructor Summary
ConstructorsConstructorDescriptionConstructs an empty JSONObjectJSONObject(Reader reader) Constructs a JSONObject with JSON data from a Reader.JSONObject(Reader reader, boolean strict) Constructs a JSONObject with JSON data from a Reader.JSONObject(String json) Constructs a JSONObject from JSON data.JSONObject(String json, boolean strict) Constructs a JSONObject from JSON data.JSONObject(Map<?, ?> map) Allows creation of a JSONObject from a Map. -
Method Summary
Modifier and TypeMethodDescriptioncompact()Removes all keys which have anullvalue.booleanIf the value already is a JSONArray, it will be casted and returned.getBoolean(String key) If the value is already a Boolean, it will be casted and returned.If the value is a Number, itsbytevalue is returned.getDate(String key, DateFormat format) If the value already is a Date, it will be casted and returned.If the value is a Number, itsdoublevalue is returned.<T extends Enum<T>>
TIf the String representation of the value equals the name of the enum constant in the given enum type, it will return the enum constant.If the value is a Number, itsfloatvalue is returned.getInteger(String key) If the value is a Number, itsintvalue is returned.If the value is a Number, itslongvalue is returned.If the value is a JSONObject already, it will be casted and returned.If the value is a Number, itsshortvalue is returned.Returns the same as the value'sObject.toString()method.toString()voidWrites this JSONObject on a given Writer.Methods inherited from class java.util.LinkedHashMap
clear, containsValue, entrySet, forEach, get, getOrDefault, keySet, replaceAll, valuesMethods inherited from class java.util.HashMap
clone, compute, computeIfAbsent, computeIfPresent, containsKey, isEmpty, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, sizeMethods inherited from class java.util.AbstractMap
hashCodeMethods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, containsKey, hashCode, isEmpty, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, size
-
Constructor Details
-
JSONObject
public JSONObject()Constructs an empty JSONObject- Since:
- 1.0.0
-
JSONObject
Allows creation of a JSONObject from a Map. After that, both the generated JSONObject and the Map can be modified independently.- Parameters:
map- the Map from which the JSONObject should be created- Since:
- 1.0.0
-
JSONObject
Constructs a JSONObject from JSON data.- Parameters:
json- the JSON data- Throws:
JSONParseException- if the JSON data is invalid- Since:
- 1.0.0
-
JSONObject
Constructs a JSONObject from JSON data.- Parameters:
json- the JSON datastrict-truefor strict validation of JSON data;falseif missing colons and commas should be tolerated- Throws:
JSONParseException- if the JSON data is invalid- Since:
- 2.1.0
-
JSONObject
Constructs a JSONObject with JSON data from a Reader.- Parameters:
reader- the Reader with the JSON data- Throws:
IOException- if an I/O error occurredJSONParseException- if the JSON is invalid- Since:
- 1.0.0
-
JSONObject
Constructs a JSONObject with JSON data from a Reader.- Parameters:
reader- the Reader with the JSON datastrict-truefor strict validation of JSON data;falseif missing colons and commas should be tolerated- Throws:
IOException- if an I/O error occurredJSONParseException- if the JSON is invalid- Since:
- 2.1.0
-
-
Method Details
-
write
Writes this JSONObject on a given Writer.- Parameters:
writer- the Writer- Throws:
IOException- if an I/O error occurred- Since:
- 1.0.0
-
compact
Removes all keys which have anullvalue.- Returns:
- a new JSONObject without any
nullvalues - Since:
- 1.1.0
-
getObject
If the value is a JSONObject already, it will be casted and returned. If the value is a Map, it will be wrapped in a JSONObject. The wrapped Map will be returned. In any other case this method returnsnull.- Parameters:
key- key of the value- Returns:
- a JSONObject or
null - Since:
- 1.0.0
-
getArray
If the value already is a JSONArray, it will be casted and returned. If the value is an array or Collection, it will be wrapped in a JSONArray. The result is returned. In any other case this method returnsnull.- Parameters:
key- key of the value- Returns:
- a JSONArray or
null - Since:
- 1.0.0
-
getBoolean
If the value is already a Boolean, it will be casted and returned. If the value is a String, it will be parsed. The result is returned. If the value is a Number, this method will returntruein case itslongvalue is1. In any other case this method returnsnull.- Parameters:
key- key of the value- Returns:
- a Boolean or
null - Since:
- 1.0.0
-
getByte
If the value is a Number, itsbytevalue is returned. If the value is a String, it will be parsed. The result is returned. If the value is a Boolean, this method returns1in case the value istrueotherwise0. In any other case this method returnsnull.- Parameters:
key- key of the value- Returns:
- a Byte or
null - Since:
- 1.0.0
-
getShort
If the value is a Number, itsshortvalue is returned. If the value is a String, it will be parsed. The result is returned. If the value is a Boolean, this method returns1in case the value istrueotherwise0. In any other case this method returnsnull.- Parameters:
key- key of the value- Returns:
- a Short or
null - Since:
- 1.0.0
-
getInteger
If the value is a Number, itsintvalue is returned. If the value is a String, it will be parsed. The result is returned. If the value is a Boolean, this method returns1in case the value istrueotherwise0. In any other case this method returnsnull.- Parameters:
key- key of the value- Returns:
- a Integer or
null - Since:
- 1.0.0
-
getLong
If the value is a Number, itslongvalue is returned. If the value is a String, it will be parsed. The result is returned. If the value is a Boolean, this method returns1in case the value istrueotherwise0. In any other case this method returnsnull.- Parameters:
key- key of the value- Returns:
- a Long or
null - Since:
- 1.0.0
-
getFloat
If the value is a Number, itsfloatvalue is returned. If the value is a String, it will be parsed. The result is returned. If the value is a Boolean, this method returns1in case the value istrueotherwise0. In any other case this method returnsnull.- Parameters:
key- key of the value- Returns:
- a Float or
null - Since:
- 1.0.0
-
getDouble
If the value is a Number, itsdoublevalue is returned. If the value is a String, it will be parsed. The result is returned. If the value is a Boolean, this method returns1in case the value istrueotherwise0. In any other case this method returnsnull.- Parameters:
key- key of the value- Returns:
- a Double or
null - Since:
- 1.0.0
-
getString
Returns the same as the value'sObject.toString()method. If the actual value isnull, this method will returnnull.- Parameters:
key- key of the value- Returns:
- a String or
null - Since:
- 1.0.0
-
getDate
If the value already is a Date, it will be casted and returned. Otherwise the result of the value'sObject.toString()will be parsed by the given DateFormat. The result is returned. If the actual value isnull, this method will returnnull.- Parameters:
key- key of the valueformat- the DateFormat to parse the date with- Returns:
- a Date or
null - Throws:
ParseException- if the date could not be parsed- Since:
- 1.0.0
-
getEnum
If the String representation of the value equals the name of the enum constant in the given enum type, it will return the enum constant. In any other case this method returnsnull.- Type Parameters:
T- the generic return type- Parameters:
key- key of the valuetype- the enum type- Returns:
- an Enum or
null - Since:
- 1.0.0
-
toString
- Overrides:
toStringin classAbstractMap<Object,Object> - Returns:
- a String representation of this JSONObject.
- Since:
- 1.0.0
-
equals
-
toXML
- Parameters:
rootName- the name of the root element- Returns:
- this JSON Object in XML
- Since:
- 1.1.0
-