Google Web Toolkit (Beta)
Search this site:

# Class JSONObject

public class JSONObject
extends JSONValue
Represents a JSON object. A JSON object is a map of string-based keys onto a set of JSONValue objects.

## Constructors

JSONObject() |
JSONObject(JavaScriptObject) | Creates a new JSONObject from the supplied JavaScript value.

## Methods

containsKey(String) | Tests whether or not this JSONObject contains the specified key.
get(String) | Gets the JSONValue associated with the specified key.
isObject() | Returns `this`, as this is a JSONObject.
keySet() | Returns keys for which this JSONObject has associations.
put(String, JSONValue) | Maps the specified key to the specified value in this JSONObject.
size() | Determines the number of keys on this object.
toString() | Converts a JSONObject into a JSON representation that can be used to communicate with a JSON service.

## Constructor Detail

### JSONObject

public JSONObject()

### JSONObject

public JSONObject(JavaScriptObject jsValue)
Creates a new JSONObject from the supplied JavaScript value.

#### Parameters

jsValue

## Method Detail

### containsKey

public boolean containsKey(String key)
Tests whether or not this JSONObject contains the specified key. We use Object.hasOwnProperty here to verify that a given key is specified on this object rather than a superclass (such as standard properties defined on Object).

#### Parameters

key
  the key to search for

#### Return Value

`true` if the JSONObject contains the specified key

### get

public JSONValue get(String key)
Gets the JSONValue associated with the specified key. We use Object.hasOwnProperty here to verify that a given key is specified on this object rather than a superclass (such as standard properties defined on Object).

#### Parameters

key
  the key to search for

#### Return Value

if found, the value associated with the specified key, or `null` otherwise

### isObject

public JSONObject isObject()
Returns `this`, as this is a JSONObject.

### keySet

public Set keySet()
Returns keys for which this JSONObject has associations.

#### Return Value

array of keys for which there is a value

### put

public JSONValue put(String key, JSONValue jsonValue)
Maps the specified key to the specified value in this JSONObject. If the specified key already has an associated value, it is overwritten.

#### Parameters

key
  the key to associate with the specified value
jsonValue
  the value to assoociate with this key

#### Return Value

if one existed, the previous value associated with the key, or `null` otherwise

### size

public int size()
Determines the number of keys on this object.

### toString

public String toString()
Converts a JSONObject into a JSON representation that can be used to communicate with a JSON service.

#### Return Value

a JSON string representation of this JSONObject instance