import org.json.*; JSONObject obj = new JSONObject(" .... "); String pageName = obj.getJSONObject("pageInfo").getString("pageName"); JSONArray arr = obj.getJSONArray("posts"); for (int i = 0; i < arr.length(); i++) { String post_id = arr.getJSONObject(i).getString("post_id"); ...... }
public Photo(JSONObject jsonPhoto) throws JSONException { this.id = jsonPhoto.getString("id"); this.owner = jsonPhoto.getString("owner"); this.title = jsonPhoto.optString("title", ""); this.server = jsonPhoto.getString("server"); this.farm = jsonPhoto.getString("farm"); this.secret = jsonPhoto.getString("secret"); }
JSONObject jo = new JSONObject(); jo.put("firstName", "John"); jo.put("lastName", "Doe"); JSONArray ja = new JSONArray(); ja.put(jo); JSONObject mainObj = new JSONObject(); mainObj.put("employees", ja);
int id; String name; JSONArray array = new JSONArray(string_of_json_array); for (int i = 0; i < array.length(); i++) { JSONObject row = array.getJSONObject(i); id = row.getInt("id"); name = row.getString("name"); }
public boolean isJSONValid(String test) { try { new JSONObject(test); } catch (JSONException ex) { // edited, to include @Arthur's comment // e.g. in case JSONArray is valid as well... try { new JSONArray(test); } catch (JSONException ex1) { return false; } } return true; }
JSONObject jsonObj = new JSONObject("{\"phonetype\":\"N95\",\"cat\":\"WP\"}");
/** * Stores an long as extra data. * @param value The value to store. * @return the Builder object for chaining operations. */ public Builder addExtra(String key, long value) { mNewExtraData.put(key, value); return this; }
/** * Get extra int value. * @param key The key of the stored value. * @return The stored value. * @throws JSONException If the mapping doesn't exist or is not the required type. */ public int getExtraInt(String key) throws JSONException { return mExtraData.getInt(key); }
/** * Get extra String value. * @param key The key of the stored value. * @return The stored value or null if it doesn't exist in specified form. */ public String optExtraString(String key) { return mExtraData.optString(key); }
/** * Get extra double value. * @param key The key of the stored value. * @return The stored value. * @throws JSONException If the mapping doesn't exist or is not the required type. */ public double getExtraDouble(String key) throws JSONException { return mExtraData.getDouble(key); }
/** * Get extra int value. * @param key The key of the stored value. * @return The stored value or 0 if it doesn't exist in specified form. */ public int optExtraInt(String key) { return mExtraData.optInt(key); }
String data = "{ ... }"; Object json = new JSONTokener(data).nextValue(); if (json instanceof JSONObject) //you have an object else if (json instanceof JSONArray) //you have an array
/** * Get extra JSONArray value. * @param key The key of the stored value. * @return The stored value or null if it doesn't exist in specified form. */ public JSONArray optExtraJSONArray(String key) { return mExtraData.optJSONArray(key); }
/** * Get extra long value. * @param key The key of the stored value. * @return The stored value or 0 if it doesn't exist in specified form. */ public long optExtraLong(String key) { return mExtraData.optLong(key); }
/** * Get extra double value. * @param key The key of the stored value. * @return The stored value or 0 if it doesn't exist in specified form. */ public double optExtraDouble(String key) { return mExtraData.optDouble(key); }
/** * Stores an int as extra data. * @param value The value to store. * @return the Builder object for chaining operations. */ public Builder addExtra(String key, int value) { mNewExtraData.put(key, value); return this; }
/** * Get extra int value. * @param key The key of the stored value. * @return The stored value. * @throws JSONException If the mapping doesn't exist or is not the required type. */ public int getExtraInt(String key) throws JSONException { return mExtraData.getInt(key); }
/** * Get extra String value. * @param key The key of the stored value. * @param defaultValue The value to return if the key does not exist or the value cannot be * coerced into the necessary type. * @return The stored value or the provided default if it doesn't exist in specified form. */ public String optExtraString(String key, String defaultValue) { return mExtraData.optString(key, defaultValue); }
/** * Stores a boolean as extra data. * @param value The value to store. * @return the Builder object for chaining operations. */ public Builder addExtra(String key, boolean value) { mNewExtraData.put(key, value); return this; }