/** * Construct a JSONObject from a subset of another JSONObject. An array of strings is used to * identify the keys that should be copied. Missing keys are ignored. * * @param jo A JSONObject. * @param names An array of strings. * @throws JSONException * @exception JSONException If a value is a non-finite number or if a name is duplicated. */ public JSONObject(JSONObject jo, String[] names) { this(); for (int i = 0; i < names.length; i += 1) { try { putOnce(names[i], jo.opt(names[i])); } catch (Exception ignore) { } } }
/** * Construct a JSONObject from a JSONTokener. * * @param x A JSONTokener object containing the source string. * @throws JSONException If there is a syntax error in the source string or a duplicated key. */ public JSONObject(JSONTokener x) throws JSONException { this(); char c; String key; if (x.nextClean() != '{') { throw x.syntaxError("A JSONObject text must begin with '{'"); } for (; ; ) { c = x.nextClean(); switch (c) { case 0: throw x.syntaxError("A JSONObject text must end with '}'"); case '}': return; default: x.back(); key = x.nextValue().toString(); } // The key is followed by ':'. We will also tolerate '=' or '=>'. c = x.nextClean(); if (c == '=') { if (x.next() != '>') { x.back(); } } else if (c != ':') { throw x.syntaxError("Expected a ':' after a key"); } putOnce(key, x.nextValue()); // Pairs are separated by ','. We will also tolerate ';'. switch (x.nextClean()) { case ';': case ',': if (x.nextClean() == '}') { return; } x.back(); break; case '}': return; default: throw x.syntaxError("Expected a ',' or '}'"); } } }