|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.apache.tapestry.json.JSONObject
public class JSONObject
A JSONObject is an unordered collection of name/value pairs. Its external form is a string wrapped in curly braces with colons between the names and values, and commas between the values and names. The internal form is an object having get() and opt() methods for accessing the values by name, and put() methods for adding or replacing values by name. The values can be any of these types: Boolean, JSONArray, JSONObject, Number, String, or the JSONObject.NULL object.
The constructor can convert an external form string into an internal form Java object. The toString() method creates an external form string.
A get() method returns a value if one can be found, and throws an exception if one cannot be found. An opt() method returns a default value instead of throwing an exception, and so is useful for obtaining optional values.
The generic get() and opt() methods return an object, which you can cast or query for type. There are also typed get() and opt() methods that do type checking and type coersion for you.
The texts produced by the toString() methods are very strict. The constructors are more forgiving in the texts they will accept:
,
(comma) may appear just
before the closing brace.'
(single quote).{ } [ ] / \ : , = ; #
and if they do not look like numbers and
if they are not the reserved words true
, false
,
or null
.=
or =>
as well as
by :
;
as well as by ,
0-
(octal) or
0x-
(hex) prefix.#
Field Summary | |
---|---|
static Object |
NULL
It is sometimes more convenient and less ambiguous to have a NULL object than to use Java's null value. |
Constructor Summary | |
---|---|
JSONObject()
Construct an empty JSONObject. |
|
JSONObject(JSONObject jo,
String[] sa)
Construct a JSONObject from a subset of another JSONObject. |
|
JSONObject(JSONTokener x)
Construct a JSONObject from a JSONTokener. |
|
JSONObject(Map map)
Construct a JSONObject from a Map. |
|
JSONObject(String string)
Construct a JSONObject from a string. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public static final Object NULL
Constructor Detail |
---|
public JSONObject()
public JSONObject(JSONObject jo, String[] sa)
jo
- A JSONObject.sa
- An array of strings.public JSONObject(JSONTokener x) throws ParseException
x
- A JSONTokener object containing the source string.
ParseException
- if there is a syntax error in the source string.public JSONObject(Map map)
map
- A map object that can be used to initialize the contents of
the JSONObject.public JSONObject(String string) throws ParseException
string
- A string beginning with {
(left
brace) and ending with }
(right
brace).
ParseException
- The string must be properly formatted.Method Detail |
---|
public JSONObject accumulate(String key, Object value)
public Object get(String key)
public boolean getBoolean(String key)
public double getDouble(String key)
public int getInt(String key)
public JSONArray getJSONArray(String key)
public JSONObject getJSONObject(String key)
public String getString(String key)
public boolean has(String key)
public boolean isNull(String key)
public Iterator keys()
public int length()
public JSONArray names()
public static String numberToString(Number n)
n
- A Number
ArithmeticException
- JSON can only serialize finite numbers.public Object opt(String key)
public boolean optBoolean(String key)
public boolean optBoolean(String key, boolean defaultValue)
public double optDouble(String key)
public double optDouble(String key, double defaultValue)
public int optInt(String key)
public int optInt(String key, int defaultValue)
public JSONArray optJSONArray(String key)
public JSONObject optJSONObject(String key)
public String optString(String key)
public String optString(String key, String defaultValue)
public JSONObject put(Object key, boolean value)
public JSONObject put(Object key, double value)
public JSONObject put(Object key, long value)
public JSONObject put(Object key, int value)
public JSONObject put(Object key, Object value)
public JSONObject putOpt(String key, Object value)
public static String quote(char value)
value
-
.
public static String quote(String string)
string
- A String
public Object remove(String key)
public JSONArray toJSONArray(JSONArray names)
public String toString()
toString
in class Object
public String toString(int indentFactor)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |