canonical example by Tabnine
public void accessingJson(JSONObject json) { Object invalid = json.get("invalid"); // throws JSONException - "invalid" entry doesn't exists String name = json.getString("name"); // name = "John Brown" int number = json.getInt("name"); // throws JSONException - "name" entry isn't int int age = json.optInt("age", 42); // using default value instead of throwing exception JSONArray pets = json.getJSONArray("pets"); for (int i = 0; i < pets.length(); i++) { String pet = pets.getString(i); } }
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 JSONObject template() { JSONObject m = model(); try { if (m.getInt("type") == Consts.MODEL_STD) { return m.getJSONArray("tmpls").getJSONObject(mOrd); } else { return model().getJSONArray("tmpls").getJSONObject(0); } } catch (JSONException e) { throw new RuntimeException(e); } }
/** * An unsorted list of all deck names. */ public ArrayList<String> allNames(boolean dyn) { ArrayList<String> list = new ArrayList<>(); try { if (dyn) { for (JSONObject x : mDecks.values()) { list.add(x.getString("name")); } } else { for (JSONObject x : mDecks.values()) { if (x.getInt("dyn") == 0) { list.add(x.getString("name")); } } } } catch (JSONException e) { throw new RuntimeException(e); } return list; }
public void fromJson(JSONObject jsonObject) { try { this.id = jsonObject.getInt("id"); this.title = jsonObject.getString("title"); this.postCount = jsonObject.getInt("post_count"); } catch (JSONException e) { e.printStackTrace(); } }
static AccessToken createFromJSONObject(JSONObject jsonObject) throws JSONException { int version = jsonObject.getInt(VERSION_KEY); if (version > CURRENT_JSON_FORMAT) { throw new FacebookException("Unknown AccessToken serialization format."); } String token = jsonObject.getString(TOKEN_KEY); Date expiresAt = new Date(jsonObject.getLong(EXPIRES_AT_KEY)); JSONArray permissionsArray = jsonObject.getJSONArray(PERMISSIONS_KEY); JSONArray declinedPermissionsArray = jsonObject.getJSONArray(DECLINED_PERMISSIONS_KEY); Date lastRefresh = new Date(jsonObject.getLong(LAST_REFRESH_KEY)); AccessTokenSource source = AccessTokenSource.valueOf(jsonObject.getString(SOURCE_KEY)); String applicationId = jsonObject.getString(APPLICATION_ID_KEY); String userId = jsonObject.getString(USER_ID_KEY); Date dataAccessExpirationTime = new Date( jsonObject.optLong(DATA_ACCESS_EXPIRATION_TIME, 0)); return new AccessToken( token, applicationId, userId, Utility.jsonArrayToStringList(permissionsArray), Utility.jsonArrayToStringList(declinedPermissionsArray), source, expiresAt, lastRefresh, dataAccessExpirationTime); }
public int _deckNewLimitSingle(JSONObject g) { try { if (g.getInt("dyn") != 0) { return mReportLimit; } JSONObject c = mCol.getDecks().confForDid(g.getLong("id")); return Math.max(0, c.getJSONObject("new").getInt("perDay") - g.getJSONArray("newToday").getInt(1)); } catch (JSONException e) { throw new RuntimeException(e); } }
JSONObject json = new JSONObject(jsonString); JSONArray jArray = json.getJSONArray("list"); System.out.println("*****JARRAY*****" + jArray.length()); for(int i=0; i<jArray.length(); i++){ JSONObject json_data = jArray.getJSONObject(i); Log.i("log_tag", "_id" + json_data.getInt("account") + ", mall_name" + json_data.getString("name") + ", location" + json_data.getString("number") + ", telephone" + json_data.getString("url") + ",----" + json_data.getString("balance") + ",----" + json_data.getString("credit") + ",----" + json_data.getString("displayName") ); }
try { JSONObject jsonObj = new JSONObject(resultfromUrl); int id = jsonObj.getInt("id"); String name = jsonObj.getString("content"); } catch (JSONException e) { e.printStackTrace(); }
private String getSiteID(String tkn, String username) { Map<String,String> cookies = new HashMap<>(); cookies.put("vs", tkn); try { JSONObject j = Http.url("https://vsco.co/ajxp/" + tkn + "/2.0/sites?subdomain=" + username).cookies(cookies).getJSON(); return Integer.toString(j.getJSONArray("sites").getJSONObject(0).getInt("id")); } catch (IOException e) { LOGGER.error("Could not get site id"); return null; } }
private int _deckRevLimitSingle(JSONObject d) { try { if (d.getInt("dyn") != 0) { return mReportLimit; } JSONObject c = mCol.getDecks().confForDid(d.getLong("id")); return Math.max(0, c.getJSONObject("rev").getInt("perDay") - d.getJSONArray("revToday").getInt(1)); } catch (JSONException e) { throw new RuntimeException(e); } }
JSONObject obj = new JSONObject(jsonString); String id = obj.getString("id"); String error = obj.getString("error"); JSONObject result = obj.getJSONObject("result"); int nPeople = result.getInt("nPeople"); JSONArray people = result.getJSONArray("people"); for(int i = 0 ; i < people.length() ; i++){ JSONObject p = (JSONObject)people.get(i); String namePeople = p.getString("namePeople"); ... }
InsetPhotoBean(JSONObject photo) { try { ref = photo.getString("ref"); caption = photo.getString("caption"); url = photo.getString("url"); widthDip = photo.getJSONObject("pixel").getInt("width"); heightDip = photo.getJSONObject("pixel").getInt("height"); } catch (JSONException e) { e.printStackTrace(); } }
public ToDoItem(JSONObject jsonObject) throws JSONException { mToDoText = jsonObject.getString(TODOTEXT); mToDoDescription = jsonObject.getString(TODODESCRIPTION); mHasReminder = jsonObject.getBoolean(TODOREMINDER); mTodoColor = jsonObject.getInt(TODOCOLOR); mTodoIdentifier = UUID.fromString(jsonObject.getString(TODOIDENTIFIER)); // if(jsonObject.has(TODOLASTEDITED)){ // mLastEdited = new Date(jsonObject.getLong(TODOLASTEDITED)); // } if (jsonObject.has(TODODATE)) { mToDoDate = new Date(jsonObject.getLong(TODODATE)); } }
private JSONObject _lapseConf(Card card) { try { JSONObject conf = _cardConf(card); // normal deck if (card.getODid() == 0) { return conf.getJSONObject("lapse"); } // dynamic deck; override some attributes, use original deck for others JSONObject oconf = mCol.getDecks().confForDid(card.getODid()); JSONArray delays = conf.optJSONArray("delays"); if (delays == null) { delays = oconf.getJSONObject("lapse").getJSONArray("delays"); } JSONObject dict = new JSONObject(); // original deck dict.put("minInt", oconf.getJSONObject("lapse").getInt("minInt")); dict.put("leechFails", oconf.getJSONObject("lapse").getInt("leechFails")); dict.put("leechAction", oconf.getJSONObject("lapse").getInt("leechAction")); dict.put("mult", oconf.getJSONObject("lapse").getDouble("mult")); // overrides dict.put("delays", delays); dict.put("resched", conf.getBoolean("resched")); return dict; } catch (JSONException e) { throw new RuntimeException(e); } }
/** "Mapping of field name -> (ord, field). */ public Map<String, Pair<Integer, JSONObject>> fieldMap(JSONObject m) { JSONArray ja; try { ja = m.getJSONArray("flds"); // TreeMap<Integer, String> map = new TreeMap<Integer, String>(); Map<String, Pair<Integer, JSONObject>> result = new HashMap<>(); for (int i = 0; i < ja.length(); i++) { JSONObject f = ja.getJSONObject(i); result.put(f.getString("name"), new Pair<>(f.getInt("ord"), f)); } return result; } catch (JSONException e) { throw new RuntimeException(e); } }
public static String parseJSONResponse(String jsonResponse) { try { JSONObject json = new JSONObject(jsonResponse); // get name & id here String name = json.getString("name"); int id = json.getInt("id"); } catch (JSONException e) { e.printStackTrace(); } return name; }