Class JSONObject

    • Nested Class Summary

      • Nested classes/interfaces inherited from class java.util.AbstractMap

        java.util.AbstractMap.SimpleEntry<K extends java.lang.Object,​V extends java.lang.Object>, java.util.AbstractMap.SimpleImmutableEntry<K extends java.lang.Object,​V extends java.lang.Object>
      • Nested classes/interfaces inherited from interface java.util.Map

        java.util.Map.Entry<K extends java.lang.Object,​V extends java.lang.Object>
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static long serialVersionUID  
    • Constructor Summary

      Constructors 
      Constructor Description
      JSONObject()  
      JSONObject​(int initialCapacity)  
      JSONObject​(java.util.Map<java.lang.String,​?> map)
      Allows creation of a JSONObject from a Map.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      JSONObject appendField​(java.lang.String fieldName, java.lang.Object fieldValue)
      Puts value to object and returns this.
      static java.lang.String escape​(java.lang.String s)
      Escape quotes, \, /, \r, \n, \b, \f, \t and other control characters (U+0000 through U+001F).
      java.lang.Number getAsNumber​(java.lang.String key)
      A Simple Helper cast an Object to an Number
      java.lang.String getAsString​(java.lang.String key)
      A Simple Helper object to String
      void merge​(java.lang.Object o2)  
      void merge​(java.lang.Object o2, boolean overwrite)
      merge two JSONObject with overwrite or not overwrite = false will not overwrite existing key overwrite = true will overwrite the value with o2 of existing key
      protected static JSONArray merge​(JSONArray o1, java.lang.Object o2)  
      private static JSONArray merge​(JSONArray o1, JSONArray o2)  
      protected static JSONObject merge​(JSONObject o1, java.lang.Object o2, boolean overwrite)  
      private static JSONObject merge​(JSONObject o1, JSONObject o2, boolean overwrite)  
      java.lang.String toJSONString()
      Converts this object to JSON string representation.
      static java.lang.String toJSONString​(java.util.Map<java.lang.String,​? extends java.lang.Object> map)  
      static java.lang.String toJSONString​(java.util.Map<java.lang.String,​? extends java.lang.Object> map, JSONStyle compression)
      Convert a map to JSON text.
      java.lang.String toJSONString​(JSONStyle compression)
      Converts this object to JSON string representation with specified compression style.
      java.lang.String toString()  
      java.lang.String toString​(JSONStyle compression)  
      static void writeJSON​(java.util.Map<java.lang.String,​? extends java.lang.Object> map, java.lang.Appendable out)  
      static void writeJSON​(java.util.Map<java.lang.String,​? extends java.lang.Object> map, java.lang.Appendable out, JSONStyle compression)
      Encode a map into JSON text and write it to out.
      static void writeJSONKV​(java.lang.String key, java.lang.Object value, java.lang.Appendable out, JSONStyle compression)
      Write a Key : value entry to a stream
      void writeJSONString​(java.lang.Appendable out)
      serialize Object as json to an stream
      void writeJSONString​(java.lang.Appendable out, JSONStyle compression)
      serialize Object as json to an stream
      • Methods inherited from class java.util.HashMap

        clear, clone, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, forEach, get, getOrDefault, isEmpty, keySet, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size, values
      • Methods inherited from class java.util.AbstractMap

        equals, hashCode
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Map

        equals, hashCode
    • Constructor Detail

      • JSONObject

        public JSONObject()
      • JSONObject

        public JSONObject​(int initialCapacity)
      • JSONObject

        public JSONObject​(java.util.Map<java.lang.String,​?> map)
        Allows creation of a JSONObject from a Map. After that, both the generated JSONObject and the Map can be modified independently.
    • Method Detail

      • escape

        public static java.lang.String escape​(java.lang.String s)
        Escape quotes, \, /, \r, \n, \b, \f, \t and other control characters (U+0000 through U+001F). It's the same as JSONValue.escape() only for compatibility here.
        See Also:
        JSONValue.escape(String)
      • toJSONString

        public static java.lang.String toJSONString​(java.util.Map<java.lang.String,​? extends java.lang.Object> map)
      • toJSONString

        public static java.lang.String toJSONString​(java.util.Map<java.lang.String,​? extends java.lang.Object> map,
                                                    JSONStyle compression)
        Convert a map to JSON text. The result is a JSON object. If this map is also a JSONAware, JSONAware specific behaviours will be omitted at this top level.
        Parameters:
        map -
        Returns:
        JSON text, or "null" if map is null.
        See Also:
        JSONValue.toJSONString(Object)
      • writeJSONKV

        public static void writeJSONKV​(java.lang.String key,
                                       java.lang.Object value,
                                       java.lang.Appendable out,
                                       JSONStyle compression)
                                throws java.io.IOException
        Write a Key : value entry to a stream
        Throws:
        java.io.IOException
      • appendField

        public JSONObject appendField​(java.lang.String fieldName,
                                      java.lang.Object fieldValue)
        Puts value to object and returns this. Handy alternative to put(String key, Object value) method.
        Parameters:
        fieldName - key with which the specified value is to be associated
        fieldValue - value to be associated with the specified key
        Returns:
        this
      • getAsString

        public java.lang.String getAsString​(java.lang.String key)
        A Simple Helper object to String
        Returns:
        a value.toString() or null
      • getAsNumber

        public java.lang.Number getAsNumber​(java.lang.String key)
        A Simple Helper cast an Object to an Number
        Returns:
        a Number or null
      • writeJSON

        public static void writeJSON​(java.util.Map<java.lang.String,​? extends java.lang.Object> map,
                                     java.lang.Appendable out)
                              throws java.io.IOException
        Throws:
        java.io.IOException
      • writeJSON

        public static void writeJSON​(java.util.Map<java.lang.String,​? extends java.lang.Object> map,
                                     java.lang.Appendable out,
                                     JSONStyle compression)
                              throws java.io.IOException
        Encode a map into JSON text and write it to out. If this map is also a JSONAware or JSONStreamAware, JSONAware or JSONStreamAware specific behaviours will be ignored at this top level.
        Throws:
        java.io.IOException
        See Also:
        JSONValue.writeJSONString(Object, Appendable)
      • writeJSONString

        public void writeJSONString​(java.lang.Appendable out)
                             throws java.io.IOException
        serialize Object as json to an stream
        Specified by:
        writeJSONString in interface JSONStreamAware
        Parameters:
        out - the output to write to
        Throws:
        java.io.IOException - if I/O error occurs
      • writeJSONString

        public void writeJSONString​(java.lang.Appendable out,
                                    JSONStyle compression)
                             throws java.io.IOException
        serialize Object as json to an stream
        Specified by:
        writeJSONString in interface JSONStreamAwareEx
        Parameters:
        out - the output to write to
        compression - the JSON style for formatting
        Throws:
        java.io.IOException - if I/O error occurs
      • merge

        public void merge​(java.lang.Object o2)
      • merge

        public void merge​(java.lang.Object o2,
                          boolean overwrite)
        merge two JSONObject with overwrite or not overwrite = false will not overwrite existing key overwrite = true will overwrite the value with o2 of existing key
      • merge

        protected static JSONObject merge​(JSONObject o1,
                                          java.lang.Object o2,
                                          boolean overwrite)
      • toJSONString

        public java.lang.String toJSONString()
        Description copied from interface: JSONAware
        Converts this object to JSON string representation.
        Specified by:
        toJSONString in interface JSONAware
        Returns:
        JSON text
      • toJSONString

        public java.lang.String toJSONString​(JSONStyle compression)
        Description copied from interface: JSONAwareEx
        Converts this object to JSON string representation with specified compression style.
        Specified by:
        toJSONString in interface JSONAwareEx
        Parameters:
        compression - the JSON style for formatting
        Returns:
        JSON text
      • toString

        public java.lang.String toString​(JSONStyle compression)
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.util.AbstractMap<java.lang.String,​java.lang.Object>