FileDocCategorySizeDatePackage
JSONObject.javaAPI DocExample2902Wed Jul 26 13:30:26 BST 2006com.oreilly.ajax.client

JSONObject

public class JSONObject extends JSONValue
Class that models a JSON object.

Fields Summary
private HashMap
valueMap
Constructors Summary
public JSONObject()


    
  
Methods Summary
public booleancontainsKey(java.lang.String key)
Returns true if this JSONObject contains the requested named key.

param
key - named value to look for
return
true if the JSONObject contains the key

    return valueMap.containsKey(key);
  
public JSONValueget(java.lang.String key)
Return null if not found.

param
key
return

    return (JSONValue) valueMap.get(key);
  
public java.lang.String[]getKeys()
Returns keys for which this JSONObject has associations.

return
array of keys for which there is a value

    Set keySet = valueMap.keySet();
    String[] keys = new String[keySet.size()];
    Iterator iter = keySet.iterator();
    int i = 0;
    while (iter.hasNext()) {
      keys[i++] = (String) iter.next();
    }

    return keys;
  
public com.oreilly.ajax.client.JSONObjectisObject()

    return this;
  
public JSONValueput(java.lang.String key, JSONValue jsonValue)
Adds the named key and value pair to this JSONObject. If there was already a value associated with that key, then the previous value is returned.

param
key
return
the previous value associated with the key

    return (JSONValue) valueMap.put(key, jsonValue);
  
public java.lang.StringtoString()
Converts a JSONObject into a JSON representation that can be used to communicate with a JSON service.

return
a JSON string representation of this JSONObject instance

    Set entrySet = valueMap.entrySet();
    Iterator iter = entrySet.iterator();

    StringBuffer sb = new StringBuffer();
    sb.append("{");
    while (iter.hasNext()) {
      Entry entry = (Entry) iter.next();
      sb.append("\"");
      sb.append(entry.getKey().toString());
      sb.append("\":");
      sb.append(entry.getValue().toString());

      if (iter.hasNext()) {
        sb.append(",");
      }
    }
    sb.append("}");
    return sb.toString();