# JSONStringer

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Summary:[Ctors](https://developer.android.com/reference/org/json/JSONStringer#pubctors)\|[Methods](https://developer.android.com/reference/org/json/JSONStringer#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/org/json/JSONStringer#inhmethods)  

# JSONStringer

`
public


class
JSONStringer
`  
`

extends `[Object](https://developer.android.com/reference/java/lang/Object)`


``


`

|---|-----------------------|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | org.json.JSONStringer |

<br />

*** ** * ** ***

Implements[JSONObject.toString](https://developer.android.com/reference/org/json/JSONObject#toString())and[JSONArray.toString](https://developer.android.com/reference/org/json/JSONArray#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:

- The stringer must have exactly one top-level array or object.
- Lexical scopes must be balanced: every call to[array()](https://developer.android.com/reference/org/json/JSONStringer#array())must have a matching call to[endArray()](https://developer.android.com/reference/org/json/JSONStringer#endArray())and every call to[object()](https://developer.android.com/reference/org/json/JSONStringer#object())must have a matching call to[endObject()](https://developer.android.com/reference/org/json/JSONStringer#endObject()).
- Arrays may not contain keys (property names).
- Objects must alternate keys (property names) and values.
- Values are inserted with either literal[value](https://developer.android.com/reference/org/json/JSONStringer#value(java.lang.Object))calls, or by nesting arrays or objects.

Calls that would result in a malformed JSON string will fail with a[JSONException](https://developer.android.com/reference/org/json/JSONException).

This class provides no facility for pretty-printing (ie. indenting) output. To encode indented output, use[JSONObject.toString(int)](https://developer.android.com/reference/org/json/JSONObject#toString(int))or[JSONArray.toString(int)](https://developer.android.com/reference/org/json/JSONArray#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](https://developer.android.com/reference/org/json/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](https://developer.android.com/reference/org/json/JSONStringer#JSONStringer())`() ` |

|                                                                                                                                               ### Public methods                                                                                                                                                ||
|----------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` `[JSONStringer](https://developer.android.com/reference/org/json/JSONStringer) | ` `[array](https://developer.android.com/reference/org/json/JSONStringer#array())`() ` Begins encoding a new array.                                                                                                           |
| ` `[JSONStringer](https://developer.android.com/reference/org/json/JSONStringer) | ` `[endArray](https://developer.android.com/reference/org/json/JSONStringer#endArray())`() ` Ends encoding the current array.                                                                                                 |
| ` `[JSONStringer](https://developer.android.com/reference/org/json/JSONStringer) | ` `[endObject](https://developer.android.com/reference/org/json/JSONStringer#endObject())`() ` Ends encoding the current object.                                                                                              |
| ` `[JSONStringer](https://developer.android.com/reference/org/json/JSONStringer) | ` `[key](https://developer.android.com/reference/org/json/JSONStringer#key(java.lang.String))`(`[String](https://developer.android.com/reference/java/lang/String)` name) ` Encodes the key (property name) to this stringer. |
| ` `[JSONStringer](https://developer.android.com/reference/org/json/JSONStringer) | ` `[object](https://developer.android.com/reference/org/json/JSONStringer#object())`() ` Begins encoding a new object.                                                                                                        |
| ` `[String](https://developer.android.com/reference/java/lang/String)            | ` `[toString](https://developer.android.com/reference/org/json/JSONStringer#toString())`() ` Returns the encoded JSON string.                                                                                                 |
| ` `[JSONStringer](https://developer.android.com/reference/org/json/JSONStringer) | ` `[value](https://developer.android.com/reference/org/json/JSONStringer#value(long))`(long value) ` Encodes`value`to this stringer.                                                                                          |
| ` `[JSONStringer](https://developer.android.com/reference/org/json/JSONStringer) | ` `[value](https://developer.android.com/reference/org/json/JSONStringer#value(double))`(double value) ` Encodes`value`to this stringer.                                                                                      |
| ` `[JSONStringer](https://developer.android.com/reference/org/json/JSONStringer) | ` `[value](https://developer.android.com/reference/org/json/JSONStringer#value(boolean))`(boolean value) ` Encodes`value`to this stringer.                                                                                    |
| ` `[JSONStringer](https://developer.android.com/reference/org/json/JSONStringer) | ` `[value](https://developer.android.com/reference/org/json/JSONStringer#value(java.lang.Object))`(`[Object](https://developer.android.com/reference/java/lang/Object)` value) ` Encodes`value`.                              |

| ### Inherited methods |
|-----------------------|---|
| From class` `[java.lang.Object](https://developer.android.com/reference/java/lang/Object)` ` |--------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` `[Object](https://developer.android.com/reference/java/lang/Object)          | ` `[clone](https://developer.android.com/reference/java/lang/Object#clone())`() ` Creates and returns a copy of this object.                                                                                                                                                      | | ` boolean`                                                                     | ` `[equals](https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object))`(`[Object](https://developer.android.com/reference/java/lang/Object)` obj) ` Indicates whether some other object is "equal to" this one.                                           | | ` void`                                                                        | ` `[finalize](https://developer.android.com/reference/java/lang/Object#finalize())`() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.                                                          | | ` final `[Class](https://developer.android.com/reference/java/lang/Class)`<?>` | ` `[getClass](https://developer.android.com/reference/java/lang/Object#getClass())`() ` Returns the runtime class of this`Object`.                                                                                                                                                | | ` int`                                                                         | ` `[hashCode](https://developer.android.com/reference/java/lang/Object#hashCode())`() ` Returns a hash code value for the object.                                                                                                                                                 | | ` final void`                                                                  | ` `[notify](https://developer.android.com/reference/java/lang/Object#notify())`() ` Wakes up a single thread that is waiting on this object's monitor.                                                                                                                            | | ` final void`                                                                  | ` `[notifyAll](https://developer.android.com/reference/java/lang/Object#notifyAll())`() ` Wakes up all threads that are waiting on this object's monitor.                                                                                                                         | | ` `[String](https://developer.android.com/reference/java/lang/String)          | ` `[toString](https://developer.android.com/reference/java/lang/Object#toString())`() ` Returns a string representation of the object.                                                                                                                                            | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long,%20int))`(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed. | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long))`(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed.                   | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait())`() ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*.                                                                                             | ||

## Public constructors

### JSONStringer

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public JSONStringer ()
```

<br />

<br />

## Public methods

### array

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public JSONStringer array ()
```

Begins encoding a new array. Each call to this method must be paired with a call to[endArray()](https://developer.android.com/reference/org/json/JSONStringer#endArray()).

<br />

|                                               Returns                                                ||
|-------------------------------------------------------------------------------|-----------------------|
| [JSONStringer](https://developer.android.com/reference/org/json/JSONStringer) | this stringer. <br /> |

|                                       Throws                                       ||
|---------------------------------------------------------------------------------|---|
| [JSONException](https://developer.android.com/reference/org/json/JSONException) |   |

### endArray

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public JSONStringer endArray ()
```

Ends encoding the current array.

<br />

|                                               Returns                                                ||
|-------------------------------------------------------------------------------|-----------------------|
| [JSONStringer](https://developer.android.com/reference/org/json/JSONStringer) | this stringer. <br /> |

|                                       Throws                                       ||
|---------------------------------------------------------------------------------|---|
| [JSONException](https://developer.android.com/reference/org/json/JSONException) |   |

### endObject

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public JSONStringer endObject ()
```

Ends encoding the current object.

<br />

|                                               Returns                                                ||
|-------------------------------------------------------------------------------|-----------------------|
| [JSONStringer](https://developer.android.com/reference/org/json/JSONStringer) | this stringer. <br /> |

|                                       Throws                                       ||
|---------------------------------------------------------------------------------|---|
| [JSONException](https://developer.android.com/reference/org/json/JSONException) |   |

### key

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public JSONStringer key (String name)
```

Encodes the key (property name) to this stringer.

<br />

|                                  Parameters                                  ||
|--------|----------------------------------------------------------------------|
| `name` | `String`: the name of the forthcoming value. May not be null. <br /> |

|                                               Returns                                                ||
|-------------------------------------------------------------------------------|-----------------------|
| [JSONStringer](https://developer.android.com/reference/org/json/JSONStringer) | this stringer. <br /> |

|                                       Throws                                       ||
|---------------------------------------------------------------------------------|---|
| [JSONException](https://developer.android.com/reference/org/json/JSONException) |   |

### object

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public JSONStringer object ()
```

Begins encoding a new object. Each call to this method must be paired with a call to[endObject()](https://developer.android.com/reference/org/json/JSONStringer#endObject()).

<br />

|                                               Returns                                                ||
|-------------------------------------------------------------------------------|-----------------------|
| [JSONStringer](https://developer.android.com/reference/org/json/JSONStringer) | this stringer. <br /> |

|                                       Throws                                       ||
|---------------------------------------------------------------------------------|---|
| [JSONException](https://developer.android.com/reference/org/json/JSONException) |   |

### toString

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public 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[Object.toString](https://developer.android.com/reference/java/lang/Object#toString()), this method returns null if the stringer contains no data.

<br />

|                                                      Returns                                                      ||
|--------------------------------------------------------------------|-----------------------------------------------|
| [String](https://developer.android.com/reference/java/lang/String) | a string representation of the object. <br /> |

### value

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public JSONStringer value (long value)
```

Encodes`value`to this stringer.

<br />

|       Parameters       ||
|---------|---------------|
| `value` | `long` <br /> |

|                                               Returns                                                ||
|-------------------------------------------------------------------------------|-----------------------|
| [JSONStringer](https://developer.android.com/reference/org/json/JSONStringer) | this stringer. <br /> |

|                                       Throws                                       ||
|---------------------------------------------------------------------------------|---|
| [JSONException](https://developer.android.com/reference/org/json/JSONException) |   |

### value

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public JSONStringer value (double value)
```

Encodes`value`to this stringer.

<br />

|                                                                                                     Parameters                                                                                                     ||
|---------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `value` | `double`: a finite value. May not be[NaNs](https://developer.android.com/reference/java/lang/Double#isNaN())or[infinities](https://developer.android.com/reference/java/lang/Double#isInfinite()). <br /> |

|                                               Returns                                                ||
|-------------------------------------------------------------------------------|-----------------------|
| [JSONStringer](https://developer.android.com/reference/org/json/JSONStringer) | this stringer. <br /> |

|                                       Throws                                       ||
|---------------------------------------------------------------------------------|---|
| [JSONException](https://developer.android.com/reference/org/json/JSONException) |   |

### value

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public JSONStringer value (boolean value)
```

Encodes`value`to this stringer.

<br />

|        Parameters         ||
|---------|------------------|
| `value` | `boolean` <br /> |

|                                               Returns                                                ||
|-------------------------------------------------------------------------------|-----------------------|
| [JSONStringer](https://developer.android.com/reference/org/json/JSONStringer) | this stringer. <br /> |

|                                       Throws                                       ||
|---------------------------------------------------------------------------------|---|
| [JSONException](https://developer.android.com/reference/org/json/JSONException) |   |

### value

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public JSONStringer value (Object value)
```

Encodes`value`.

<br />

|                                                                                                                                                                                               Parameters                                                                                                                                                                                               ||
|---------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `value` | `Object`: a[JSONObject](https://developer.android.com/reference/org/json/JSONObject),[JSONArray](https://developer.android.com/reference/org/json/JSONArray), String, Boolean, Integer, Long, Double or null. May not be[NaNs](https://developer.android.com/reference/java/lang/Double#isNaN())or[infinities](https://developer.android.com/reference/java/lang/Double#isInfinite()). <br /> |

|                                               Returns                                                ||
|-------------------------------------------------------------------------------|-----------------------|
| [JSONStringer](https://developer.android.com/reference/org/json/JSONStringer) | this stringer. <br /> |

|                                       Throws                                       ||
|---------------------------------------------------------------------------------|---|
| [JSONException](https://developer.android.com/reference/org/json/JSONException) |   |