Package net.minidev.json
Class JSONObject
- java.lang.Object
-
- java.util.AbstractMap<K,V>
-
- java.util.HashMap<java.lang.String,java.lang.Object>
-
- net.minidev.json.JSONObject
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Cloneable,java.util.Map<java.lang.String,java.lang.Object>,JSONAware,JSONAwareEx,JSONStreamAware,JSONStreamAwareEx
public class JSONObject extends java.util.HashMap<java.lang.String,java.lang.Object> implements JSONAwareEx, JSONStreamAwareEx
A JSON object. Key value pairs are unordered. JSONObject supports java.util.Map interface.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private static longserialVersionUID
-
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 JSONObjectappendField(java.lang.String fieldName, java.lang.Object fieldValue)Puts value to object and returns this.static java.lang.Stringescape(java.lang.String s)Escape quotes, \, /, \r, \n, \b, \f, \t and other control characters (U+0000 through U+001F).java.lang.NumbergetAsNumber(java.lang.String key)A Simple Helper cast an Object to an Numberjava.lang.StringgetAsString(java.lang.String key)A Simple Helper object to Stringvoidmerge(java.lang.Object o2)voidmerge(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 keyprotected static JSONArraymerge(JSONArray o1, java.lang.Object o2)private static JSONArraymerge(JSONArray o1, JSONArray o2)protected static JSONObjectmerge(JSONObject o1, java.lang.Object o2, boolean overwrite)private static JSONObjectmerge(JSONObject o1, JSONObject o2, boolean overwrite)java.lang.StringtoJSONString()Converts this object to JSON string representation.static java.lang.StringtoJSONString(java.util.Map<java.lang.String,? extends java.lang.Object> map)static java.lang.StringtoJSONString(java.util.Map<java.lang.String,? extends java.lang.Object> map, JSONStyle compression)Convert a map to JSON text.java.lang.StringtoJSONString(JSONStyle compression)Converts this object to JSON string representation with specified compression style.java.lang.StringtoString()java.lang.StringtoString(JSONStyle compression)static voidwriteJSON(java.util.Map<java.lang.String,? extends java.lang.Object> map, java.lang.Appendable out)static voidwriteJSON(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 voidwriteJSONKV(java.lang.String key, java.lang.Object value, java.lang.Appendable out, JSONStyle compression)Write a Key : value entry to a streamvoidwriteJSONString(java.lang.Appendable out)serialize Object as json to an streamvoidwriteJSONString(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
-
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
-
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.IOExceptionWrite 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 associatedfieldValue- 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.IOExceptionEncode 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.IOExceptionserialize Object as json to an stream- Specified by:
writeJSONStringin interfaceJSONStreamAware- 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.IOExceptionserialize Object as json to an stream- Specified by:
writeJSONStringin interfaceJSONStreamAwareEx- Parameters:
out- the output to write tocompression- 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)
-
merge
private static JSONObject merge(JSONObject o1, JSONObject o2, boolean overwrite)
-
toJSONString
public java.lang.String toJSONString()
Description copied from interface:JSONAwareConverts this object to JSON string representation.- Specified by:
toJSONStringin interfaceJSONAware- Returns:
- JSON text
-
toJSONString
public java.lang.String toJSONString(JSONStyle compression)
Description copied from interface:JSONAwareExConverts this object to JSON string representation with specified compression style.- Specified by:
toJSONStringin interfaceJSONAwareEx- 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:
toStringin classjava.util.AbstractMap<java.lang.String,java.lang.Object>
-
-