protected static JSONObject parseJsonPart(Reader reader) throws JSONException { return (JSONObject)(new JSONTokener(reader).nextValue()); }
@Override public Object parse(String json) throws InvalidJsonException { try { return new JSONTokener(json).nextValue(); } catch (JSONException e) { throw new InvalidJsonException(e); } }
@Override public Object parse(InputStream jsonStream, String charset) throws InvalidJsonException { try { return new JSONTokener(new InputStreamReader(jsonStream, charset)).nextValue(); } catch (UnsupportedEncodingException e) { throw new JsonPathException(e); } catch (JSONException e) { throw new InvalidJsonException(e); } }
/** * Creates a new {@code JSONObject} with name/value mappings from the next object in the tokener. * * @param readFrom a tokener whose nextValue() method will yield a {@code JSONObject}. * @throws JSONException if the parse fails or doesn't yield a {@code JSONObject}. */ public JSONObject(JSONTokener readFrom) throws JSONException { /* * Getting the parser to populate this could get tricky. Instead, just parse to temporary * JSONObject and then steal the data from that. */ Object object = readFrom.nextValue(); if (object instanceof JSONObject) { this.nameValuePairs = ((JSONObject) object).nameValuePairs; } else { throw JSON.typeMismatch(object, "JSONObject"); } }
/** * Creates a new {@code JSONArray} with values from the next array in the tokener. * * @param readFrom a tokener whose nextValue() method will yield a {@code JSONArray}. * @throws JSONException if the parse fails or doesn't yield a {@code JSONArray}. */ public JSONArray(JSONTokener readFrom) throws JSONException { /* * Getting the parser to populate this could get tricky. Instead, just parse to temporary * JSONArray and then steal the data from that. */ Object object = readFrom.nextValue(); if (object instanceof JSONArray) { values = ((JSONArray) object).values; } else { throw JSON.typeMismatch(object, "JSONArray"); } }
/** * Creates a new {@code JSONArray} with values from the next array in the * tokener. * * @param readFrom a tokener whose nextValue() method will yield a * {@code JSONArray}. * @throws JSONException if the parse fails or doesn't yield a * {@code JSONArray}. */ public JSONArray(JSONTokener readFrom) throws JSONException { /* * Getting the parser to populate this could get tricky. Instead, just * parse to temporary JSONArray and then steal the data from that. */ Object object = readFrom.nextValue(); if (object instanceof JSONArray) { values = ((JSONArray) object).values; } else { throw JSON.typeMismatch(object, "JSONArray"); } }
/** * Creates a new {@code JSONObject} with name/value mappings from the next * object in the tokener. * * @param readFrom a tokener whose nextValue() method will yield a * {@code JSONObject}. * @throws JSONException if the parse fails or doesn't yield a * {@code JSONObject}. */ public JSONObject(JSONTokener readFrom) throws JSONException { /* * Getting the parser to populate this could get tricky. Instead, just * parse to temporary JSONObject and then steal the data from that. */ Object object = readFrom.nextValue(); if (object instanceof JSONObject) { this.nameValuePairs = ((JSONObject) object).nameValuePairs; } else { throw JSON.typeMismatch(object, "JSONObject"); } }
private List<EarthQuakeRec> parseJsonString(String data) { String LONGITUDE_TAG = "lng"; String LATITUDE_TAG = "lat"; String MAGNITUDE_TAG = "magnitude"; String EARTHQUAKE_TAG = "earthquakes"; List<EarthQuakeRec> result = new ArrayList<>(); try { // Get top-level JSON Object - a Map JSONObject responseObject = (JSONObject) new JSONTokener( data).nextValue(); // Extract value of "earthquakes" key -- a List JSONArray earthquakes = responseObject .getJSONArray(EARTHQUAKE_TAG); // Iterate over earthquakes list for (int idx = 0; idx < earthquakes.length(); idx++) { // Get single earthquake mData - a Map JSONObject earthquake = (JSONObject) earthquakes.get(idx); // Summarize earthquake mData as a string and add it to // result result.add(new EarthQuakeRec( Double.valueOf(earthquake.get(LATITUDE_TAG).toString()), Double.valueOf(earthquake.get(LONGITUDE_TAG).toString()), Double.valueOf(earthquake.get(MAGNITUDE_TAG).toString()))); } } catch (JSONException e) { e.printStackTrace(); } return result; } }
public String toJson() { JSONObject jsonObject= new JSONObject(); try { jsonObject.put(CALLBACK_ID_STR, getCallbackId()); jsonObject.put(DATA_STR, getData()); jsonObject.put(HANDLER_NAME_STR, getHandlerName()); String data = getResponseData(); if (TextUtils.isEmpty(data)) { jsonObject.put(RESPONSE_DATA_STR, data); } else { jsonObject.put(RESPONSE_DATA_STR, new JSONTokener(data).nextValue()); } jsonObject.put(RESPONSE_DATA_STR, getResponseData()); jsonObject.put(RESPONSE_ID_STR, getResponseId()); return jsonObject.toString(); } catch (JSONException e) { e.printStackTrace(); } return null; }
public ArrayList<ToDoItem> loadFromFile() throws IOException, JSONException { ArrayList<ToDoItem> items = new ArrayList<>(); BufferedReader bufferedReader = null; FileInputStream fileInputStream = null; try { fileInputStream = mContext.openFileInput(mFileName); StringBuilder builder = new StringBuilder(); String line; bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream)); while ((line = bufferedReader.readLine()) != null) { builder.append(line); } JSONArray jsonArray = (JSONArray) new JSONTokener(builder.toString()).nextValue(); for (int i = 0; i < jsonArray.length(); i++) { ToDoItem item = new ToDoItem(jsonArray.getJSONObject(i)); items.add(item); } } catch (FileNotFoundException fnfe) { //do nothing about it //file won't exist first time app is run } finally { if (bufferedReader != null) { bufferedReader.close(); } if (fileInputStream != null) { fileInputStream.close(); } } return items; }
private Object parse(String json) throws JSONException { JSONTokener tokener = new JSONTokener(json); char c = tokener.nextClean(); //peak ahead tokener.back(); //revert if (c == '{') { //json object JSONObject o = new JSONObject(tokener); return toMap(o); } else if (c == '[') { JSONArray a = new JSONArray(tokener); return toList(a); } else { //raw json value Object value = tokener.nextValue(); return convertIfNecessary(value); } }
static List<GraphResponse> createResponsesFromString( String responseString, HttpURLConnection connection, GraphRequestBatch requests ) throws FacebookException, JSONException, IOException { JSONTokener tokener = new JSONTokener(responseString); Object resultObject = tokener.nextValue(); List<GraphResponse> responses = createResponsesFromObject( connection, requests, resultObject); Logger.log( LoggingBehavior.REQUESTS, RESPONSE_LOG_TAG, "Response\n Id: %s\n Size: %d\n Responses:\n%s\n", requests.getId(), responseString.length(), responses); return responses; }
JSONTokener tokener = new JSONTokener(headerString); try { Object parsed = tokener.nextValue(); if (!(parsed instanceof JSONObject)) { Logger.log(
public static Object getStringPropertyAsJSON( JSONObject jsonObject, String key, String nonJSONPropertyKey ) throws JSONException { Object value = jsonObject.opt(key); if (value != null && value instanceof String) { JSONTokener tokener = new JSONTokener((String) value); value = tokener.nextValue(); } if (value != null && !(value instanceof JSONObject || value instanceof JSONArray)) { if (nonJSONPropertyKey != null) { // Facebook sometimes gives us back a non-JSON value such as // literal "true" or "false" as a result. // If we got something like that, we present it to the caller as a JSONObject // with a single property. We only do this if the caller wants that behavior. jsonObject = new JSONObject(); jsonObject.putOpt(nonJSONPropertyKey, value); return jsonObject; } else { throw new FacebookException("Got an unexpected non-JSON object."); } } return value; }
private JSONArray getJsonArrayFromURL(URL url) throws IOException { // Wait 2 seconds before the next request long timeDiff = System.currentTimeMillis() - lastRequestTime; if (timeDiff < SLEEP_TIME) { try { Thread.sleep(timeDiff); } catch (InterruptedException e) { LOGGER.warn("[!] Interrupted while waiting to load next page", e); return new JSONArray(); } } lastRequestTime = System.currentTimeMillis(); String jsonString = Http.url(url) .ignoreContentType() .userAgent(REDDIT_USER_AGENT) .response() .body(); Object jsonObj = new JSONTokener(jsonString).nextValue(); JSONArray jsonArray = new JSONArray(); if (jsonObj instanceof JSONObject) { jsonArray.put(jsonObj); } else if (jsonObj instanceof JSONArray) { jsonArray = (JSONArray) jsonObj; } else { LOGGER.warn("[!] Unable to parse JSON: " + jsonString); } return jsonArray; }
private List<JSONObject> getTweets(String url) throws IOException { List<JSONObject> tweets = new ArrayList<>(); LOGGER.info(" Retrieving " + url); Document doc = Http.url(url) .ignoreContentType() .header("Authorization", "Bearer " + accessToken) .header("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8") .header("User-agent", "ripe and zipe") .get(); String body = doc.body().html().replaceAll(""", "\""); Object jsonObj = new JSONTokener(body).nextValue(); JSONArray statuses; if (jsonObj instanceof JSONObject) { JSONObject json = (JSONObject) jsonObj; if (json.has("errors")) { String msg = json.getJSONObject("errors").getString("message"); throw new IOException("Twitter responded with errors: " + msg); } statuses = json.getJSONArray("statuses"); } else { statuses = (JSONArray) jsonObj; } for (int i = 0; i < statuses.length(); i++) { tweets.add((JSONObject) statuses.get(i)); } return tweets; }
result.put(nextValue());
} else { x.back(); this.myArrayList.add(x.nextValue());
Object obj = tokener.nextValue(); if (!(obj instanceof JSONObject)) { fail(errorMessage + "could not deserialize a JSONObject");