/** * Construct a JSONObject from a string. * This is the most commonly used JSONObject constructor. * @param string A string beginning * with <code>{</code> <small>(left brace)</small> and ending * with <code>}</code> <small>(right brace)</small>. * @exception JSONException If there is a syntax error in the source string. */ public JSONObject(String string) throws JSONException { this(new JSONTokener(string)); }
/** * Construct a JSONArray from a source sJSON text. * @param string A string that begins with * <code>[</code> <small>(left bracket)</small> * and ends with <code>]</code> <small>(right bracket)</small>. * @throws JSONException If there is a syntax error. */ public JSONArray(String string) throws JSONException { this(new JSONTokener(string)); }
/** * Produce a JSONArray of JSONObjects from a comma delimited text string, * using the first row as a source of names. * @param string The comma delimited text. * @return A JSONArray of JSONObjects. * @throws JSONException */ public static JSONArray toJSONArray(String string) throws JSONException { return toJSONArray(new JSONTokener(string)); }
/** * Produce a JSONArray of JSONObjects from a comma delimited text string * using a supplied JSONArray as the source of element names. * @param names A JSONArray of strings. * @param string The comma delimited text. * @return A JSONArray of JSONObjects. * @throws JSONException */ public static JSONArray toJSONArray(JSONArray names, String string) throws JSONException { return toJSONArray(names, new JSONTokener(string)); }
/** * Strictly validate the JSON text * @param text The text to check. * @throws JSONException If the text is not valid. */ public static void validate(final String text) throws JSONException { JSONTokener x = new JSONTokener(text); validate(x); // make sure nothing more is present after last array or object char c = x.nextClean(); if ( c != 0 ) { throw x.syntaxError("Unexpected '" + c + "' at end of file."); } }
/** * Get the JSONArray associated with an index. * @param index The index must be between 0 and length() - 1. * @return A JSONArray value. * @throws JSONException If there is no value for the index. or if the * value is not a JSONArray */ public JSONArray getJSONArray(int index) throws JSONException { Object o = get(index); if (o instanceof JSONArray) { return (JSONArray) o; } else if (o instanceof String) { JSONTokener tokener = new JSONTokener((String) o); try { return new JSONArray(tokener); } catch (JSONException ignore) { // will throw the appropriate exception below } } throw new JSONException("JSONArray[" + index + "] is not a JSONArray."); }
/** * Get the JSONObject associated with an index. * @param index subscript * @return A JSONObject value. * @throws JSONException If there is no value for the index or if the * value is not a JSONObject */ public JSONObject getJSONObject(int index) throws JSONException { Object o = get(index); if (o instanceof JSONObject) { return (JSONObject) o; } else if (o instanceof String) { JSONTokener tokener = new JSONTokener((String) o); try { return new JSONObject(tokener); } catch (JSONException ignore) { // will throw the appropriate exception below } } throw new JSONException("JSONArray[" + index + "] is not a JSONObject."); }
/** * Convert a cookie list into a JSONObject. A cookie list is a sequence * of name/value pairs. The names are separated from the values by '='. * The pairs are separated by ';'. The names and the values * will be unescaped, possibly converting '+' and '%' sequences. * * To add a cookie to a cooklist, * cookielistJSONObject.put(cookieJSONObject.getString("name"), * cookieJSONObject.getString("value")); * @param string A cookie list string * @return A JSONObject * @throws JSONException */ public static JSONObject toJSONObject(String string) throws JSONException { JSONObject o = new JSONObject(); JSONTokener x = new JSONTokener(string); while (x.more()) { String name = Cookie.unescape(x.nextTo('=')); x.next('='); o.put(name, Cookie.unescape(x.nextTo(';'))); x.next(); } return o; }
JSONObject o = new JSONObject(); Object v; JSONTokener x = new JSONTokener(string); o.put("name", x.nextTo('=')); x.next('=');