Most visited

Recently visited

Added in API level 1

JSONStringer

public class JSONStringer
extends Object

java.lang.Object
   ↳ org.json.JSONStringer


Implements toString() and toString(). Most application developers should use those methods directly and disregard this API. For example:

 JSONObject object = ...
 String json = object.toString();

Stringers only encode well-formed JSON strings. In particular:

Calls that would result in a malformed JSON string will fail with a JSONException.

This class provides no facility for pretty-printing (ie. indenting) output. To encode indented output, use toString(int) or toString(int).

Some implementations of the API support at most 20 levels of nesting. Attempts to create more than 20 levels of nesting may fail with a JSONException.

Each stringer may be used to encode a single top level value. Instances of this class are not thread safe. Although this class is nonfinal, it was not designed for inheritance and should not be subclassed. In particular, self-use by overrideable methods is not specified. See Effective Java Item 17, "Design and Document or inheritance or else prohibit it" for further information.

Summary

Public constructors

JSONStringer()

Public methods

JSONStringer array()

Begins encoding a new array.

JSONStringer endArray()

Ends encoding the current array.

JSONStringer endObject()

Ends encoding the current object.

JSONStringer key(String name)

Encodes the key (property name) to this stringer.

JSONStringer object()

Begins encoding a new object.

String toString()

Returns the encoded JSON string.

JSONStringer value(long value)

Encodes value to this stringer.

JSONStringer value(double value)

Encodes value to this stringer.

JSONStringer value(boolean value)

Encodes value to this stringer.

JSONStringer value(Object value)

Encodes value.

Inherited methods

From class java.lang.Object

Public constructors

JSONStringer

Added in API level 1
JSONStringer ()

Public methods

array

Added in API level 1
JSONStringer array ()

Begins encoding a new array. Each call to this method must be paired with a call to endArray().

Returns
JSONStringer this stringer.
Throws
JSONException

endArray

Added in API level 1
JSONStringer endArray ()

Ends encoding the current array.

Returns
JSONStringer this stringer.
Throws
JSONException

endObject

Added in API level 1
JSONStringer endObject ()

Ends encoding the current object.

Returns
JSONStringer this stringer.
Throws
JSONException

key

Added in API level 1
JSONStringer key (String name)

Encodes the key (property name) to this stringer.

Parameters
name String: the name of the forthcoming value. May not be null.
Returns
JSONStringer this stringer.
Throws
JSONException

object

Added in API level 1
JSONStringer object ()

Begins encoding a new object. Each call to this method must be paired with a call to endObject().

Returns
JSONStringer this stringer.
Throws
JSONException

toString

Added in API level 1
String toString ()

Returns the encoded JSON string.

If invoked with unterminated arrays or unclosed objects, this method's return value is undefined.

Warning: although it contradicts the general contract of toString(), this method returns null if the stringer contains no data.

Returns
String a string representation of the object.

value

Added in API level 1
JSONStringer value (long value)

Encodes value to this stringer.

Parameters
value long
Returns
JSONStringer this stringer.
Throws
JSONException

value

Added in API level 1
JSONStringer value (double value)

Encodes value to this stringer.

Parameters
value double: a finite value. May not be NaNs or infinities.
Returns
JSONStringer this stringer.
Throws
JSONException

value

Added in API level 1
JSONStringer value (boolean value)

Encodes value to this stringer.

Parameters
value boolean
Returns
JSONStringer this stringer.
Throws
JSONException

value

Added in API level 1
JSONStringer value (Object value)

Encodes value.

Parameters
value Object: a JSONObject, JSONArray, String, Boolean, Integer, Long, Double or null. May not be NaNs or infinities.
Returns
JSONStringer this stringer.
Throws
JSONException

Hooray!