Class JSONObject

All Implemented Interfaces:
Serializable, Cloneable, Map<Object,Object>

public class JSONObject extends LinkedHashMap<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:
  • Constructor Details

  • Method Details

    • write

      public void write(Writer writer) throws IOException
      Writes this JSONObject on a given Writer.
      Parameters:
      writer - the Writer
      Throws:
      IOException - if an I/O error occurred
      Since:
      1.0.0
    • compact

      public JSONObject compact()
      Removes all keys which have a null value.
      Returns:
      a new JSONObject without any null values
      Since:
      1.1.0
    • getObject

      public JSONObject getObject(String key)
      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 returns null.
      Parameters:
      key - key of the value
      Returns:
      a JSONObject or null
      Since:
      1.0.0
    • getArray

      public JSONArray getArray(String key)
      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 returns null.
      Parameters:
      key - key of the value
      Returns:
      a JSONArray or null
      Since:
      1.0.0
    • getBoolean

      public Boolean getBoolean(String key)
      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 return true in case its long value is 1. In any other case this method returns null.
      Parameters:
      key - key of the value
      Returns:
      a Boolean or null
      Since:
      1.0.0
    • getByte

      public Byte getByte(String key)
      If the value is a Number, its byte value is returned. If the value is a String, it will be parsed. The result is returned. If the value is a Boolean, this method returns 1 in case the value is true otherwise 0. In any other case this method returns null.
      Parameters:
      key - key of the value
      Returns:
      a Byte or null
      Since:
      1.0.0
    • getShort

      public Short getShort(String key)
      If the value is a Number, its short value is returned. If the value is a String, it will be parsed. The result is returned. If the value is a Boolean, this method returns 1 in case the value is true otherwise 0. In any other case this method returns null.
      Parameters:
      key - key of the value
      Returns:
      a Short or null
      Since:
      1.0.0
    • getInteger

      public Integer getInteger(String key)
      If the value is a Number, its int value is returned. If the value is a String, it will be parsed. The result is returned. If the value is a Boolean, this method returns 1 in case the value is true otherwise 0. In any other case this method returns null.
      Parameters:
      key - key of the value
      Returns:
      a Integer or null
      Since:
      1.0.0
    • getLong

      public Long getLong(String key)
      If the value is a Number, its long value is returned. If the value is a String, it will be parsed. The result is returned. If the value is a Boolean, this method returns 1 in case the value is true otherwise 0. In any other case this method returns null.
      Parameters:
      key - key of the value
      Returns:
      a Long or null
      Since:
      1.0.0
    • getFloat

      public Float getFloat(String key)
      If the value is a Number, its float value is returned. If the value is a String, it will be parsed. The result is returned. If the value is a Boolean, this method returns 1 in case the value is true otherwise 0. In any other case this method returns null.
      Parameters:
      key - key of the value
      Returns:
      a Float or null
      Since:
      1.0.0
    • getDouble

      public Double getDouble(String key)
      If the value is a Number, its double value is returned. If the value is a String, it will be parsed. The result is returned. If the value is a Boolean, this method returns 1 in case the value is true otherwise 0. In any other case this method returns null.
      Parameters:
      key - key of the value
      Returns:
      a Double or null
      Since:
      1.0.0
    • getString

      public String getString(String key)
      Returns the same as the value's Object.toString() method. If the actual value is null, this method will return null.
      Parameters:
      key - key of the value
      Returns:
      a String or null
      Since:
      1.0.0
    • getDate

      public Date getDate(String key, DateFormat format) throws ParseException
      If the value already is a Date, it will be casted and returned. Otherwise the result of the value's Object.toString() will be parsed by the given DateFormat. The result is returned. If the actual value is null, this method will return null.
      Parameters:
      key - key of the value
      format - 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

      public <T extends Enum<T>> T getEnum(String key, Class<T> type)
      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 returns null.
      Type Parameters:
      T - the generic return type
      Parameters:
      key - key of the value
      type - the enum type
      Returns:
      an Enum or null
      Since:
      1.0.0
    • toString

      public String toString()
      Overrides:
      toString in class AbstractMap<Object,Object>
      Returns:
      a String representation of this JSONObject.
      Since:
      1.0.0
    • equals

      public boolean equals(Object object)
      Specified by:
      equals in interface Map<Object,Object>
      Overrides:
      equals in class AbstractMap<Object,Object>
    • toXML

      public String toXML(String rootName)
      Parameters:
      rootName - the name of the root element
      Returns:
      this JSON Object in XML
      Since:
      1.1.0