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); } }
public List<String> getArrayValues(String key) { List<String> result = new ArrayList<>(); if (jsonObject.has(key)) { JSONArray jsonArray = jsonObject.getJSONArray(key); for (int i = 0; i < jsonArray.length(); i++) { result.add(jsonArray.getString(i)); } } return result; }
try { JSONArray jr = new JSONArray("Your json string"); JSONObject jb = (JSONObject)jr.getJSONObject(0); JSONArray st = jb.getJSONArray("streets"); for(int i=0;i<st.length();i++) { String street = st.getString(i); Log.i("..........",""+street); // loop and add it to array or arraylist } }catch(Exception e) { e.printStackTrace(); }
/** * Produce a JSONObject by combining a JSONArray of names with the values of * this JSONArray. * * @param names * A JSONArray containing a list of key strings. These will be * paired with the values. * @return A JSONObject, or null if there are no names or if this JSONArray * has no values. * @throws JSONException * If any of the names are null. */ public JSONObject toJSONObject(JSONArray names) throws JSONException { if (names == null || names.length() == 0 || this.length() == 0) { return null; } JSONObject jo = new JSONObject(); for (int i = 0; i < names.length(); i += 1) { jo.put(names.getString(i), this.opt(i)); } return jo; }
for (int i = 0; i < array.length(); ++i) { if (arrayClass.isEnum()) { objectList.add(getEnumValue(array.getString(i), arrayClass)); } else if (canDirectlySerializeClass(arrayClass)) { objectList.add(array.get(i)); } else { JSONObject jsonObject = array.getJSONObject(i); if (jsonObject == null) { objectList.add(null);
private List<Partition> partitions() throws JSONException, TException { if (tableDesc == null) { return null; } // TODO : jackson-streaming-iterable-redo this JSONArray jsonPartitions = new JSONArray(json.getString(PartitionSerializer.FIELD_NAME)); List<Partition> partitionsList = new ArrayList<>(jsonPartitions.length()); for (int i = 0; i < jsonPartitions.length(); ++i) { String partDesc = jsonPartitions.getString(i); partitionsList.add(deserialize(new Partition(), partDesc)); } return partitionsList; }
mOptions = mCol.getDecks().confForDid(mDeck.getLong("id")); mValues.put("name", mDeck.getString("name")); mValues.put("desc", mDeck.getString("desc")); mValues.put("deckConf", mDeck.getString("conf")); mValues.put("maxAnswerTime", mOptions.getString("maxTaken")); mValues.put("newSteps", StepsPreference.convertFromJSON(newOptions.getJSONArray("delays"))); mValues.put("newGradIvl", newOptions.getJSONArray("ints").getString(0)); mValues.put("newEasy", newOptions.getJSONArray("ints").getString(1)); mValues.put("newFactor", Integer.toString(newOptions.getInt("initialFactor") / 10)); mValues.put("newOrder", newOptions.getString("order"));
protected void cacheValues() { Timber.d("cacheValues()"); try { JSONArray ar = mDeck.getJSONArray("terms").getJSONArray(0); mValues.put("search", ar.getString(0)); mValues.put("limit", ar.getString(1)); mValues.put("order", ar.getString(2)); JSONArray delays = mDeck.optJSONArray("delays"); if (delays != null) { mValues.put("steps", StepsPreference.convertFromJSON(delays)); mValues.put("stepsOn", Boolean.toString(true)); } else { mValues.put("steps", "1 10"); mValues.put("stepsOn", Boolean.toString(false)); } mValues.put("resched", Boolean.toString(mDeck.getBoolean("resched"))); } catch (JSONException e) { throw new RuntimeException(e); } }
@Override protected List<String> getURLsFromJSON(JSONObject json) { List<String> assetURLs = new ArrayList<>(); JSONArray currentObject; String chapterHash = json.getString("hash"); // Server is the cdn hosting the images. String server = json.getString("server"); for (int i = 0; i < json.getJSONArray("page_array").length(); i++) { currentObject = json.getJSONArray("page_array"); assetURLs.add(getImageUrl(chapterHash, currentObject.getString(i), server)); } return assetURLs; }
/** * Load registry from JSON. */ public void load(String json) { mChanged = false; mModels = new HashMap<>(); try { JSONObject modelarray = new JSONObject(json); JSONArray ids = modelarray.names(); if (ids != null) { for (int i = 0; i < ids.length(); i++) { String id = ids.getString(i); JSONObject o = modelarray.getJSONObject(id); mModels.put(o.getLong("id"), o); } } } catch (JSONException e) { throw new RuntimeException(e); } }
private static String getChangeList(JSONObject rj) { JSONArray jsonChangeList = rj.getJSONArray("changeList"); StringBuilder changeList = new StringBuilder(); for (int i = 0; i < jsonChangeList.length(); i++) { String change = jsonChangeList.getString(i); if (change.startsWith(UpdateUtils.getThisJarVersion() + ":")) { break; } changeList.append("\n").append(change); } return changeList.toString(); }
for (int i = 0; i < findings.length(); i++) { final JSONObject finding = findings.getJSONObject(i); final String version = finding.optString("version", null); final JSONArray paths = finding.optJSONArray("paths"); for (int j = 0; j < paths.length(); j++) { final String path = paths.getString(j); if (path != null && path.equals(advisory.getModuleName())) { advisory.setVersion(version); final List<String> stringCves = new ArrayList<>(); if (jsonCves != null) { for (int j = 0; j < jsonCves.length(); j++) { stringCves.add(jsonCves.getString(j));
JSONArray names = json.names(); JSONArray values = json.toJSONArray(names); for(int i=0; i<values.length(); i++){ if (names.getString(i).equals("description")){ setDescription(values.getString(i)); } else if (names.getString(i).equals("expiryDate")){ String dateString = values.getString(i); setExpiryDate(stringToDateHelper(dateString)); } else if (names.getString(i).equals("id")){ setId(values.getLong(i)); } else if (names.getString(i).equals("offerCode")){ setOfferCode(values.getString(i)); } else if (names.getString(i).equals("startDate")){ String dateString = values.getString(i); setStartDate(stringToDateHelper(dateString)); } else if (names.getString(i).equals("title")){ setTitle(values.getString(i)); } }
final String latestVersionName = versionMap.get(latestVersionNumber).getString("versionName"); final Uri downloadUri = Uri.parse(pkgInfo.getString("downloadUrl")); final JSONArray versionChangelog = versionInfo.optJSONArray("changelog"); if (versionChangelog != null) { final int len = versionChangelog.length(); for (int i = 0; i < len; i++) { changelog.add(versionChangelog.getString(i));
assertNotNull(target.getEffectiveDate()); JSONObject jsonEndpoint = target.toJSONObject(); assertTrue(jsonEndpoint.getString("ApplicationId") .equalsIgnoreCase(target.getApplicationId())); assertTrue(jsonEndpoint.getString("EndpointId") .equalsIgnoreCase(target.getEndpointId())); assertTrue(jsonEndpoint.getString("ChannelType") .equalsIgnoreCase(target.getChannelType())); assertTrue(jsonEndpoint.getString("EffectiveDate") JSONArray attrValues = attributes.getJSONArray("key1"); assertTrue(attrValues.getString(0).equalsIgnoreCase("attr1")); assertTrue(attrValues.getString(1).equalsIgnoreCase("attr2"));
private List<Long> _fillDyn(JSONObject deck) { JSONArray terms; List<Long> ids; try { terms = deck.getJSONArray("terms").getJSONArray(0); String search = terms.getString(0); int limit = terms.getInt(1); int order = terms.getInt(2); String orderlimit = _dynOrder(order, limit); if (!TextUtils.isEmpty(search.trim())) { search = String.format(Locale.US, "(%s)", search); } search = String.format(Locale.US, "%s -is:suspended -is:buried -deck:filtered", search); ids = mCol.findCards(search, orderlimit); if (ids.isEmpty()) { return ids; } // move the cards over mCol.log(deck.getLong("id"), ids); _moveToDyn(deck.getLong("id"), ids); } catch (JSONException e) { throw new RuntimeException(e); } return ids; }
private ArrayList<JSONObject> _tmplsFromOrds(JSONObject model, ArrayList<Integer> avail) { ArrayList<JSONObject> ok = new ArrayList<>(); JSONArray tmpls; try { if (model.getInt("type") == Consts.MODEL_STD) { tmpls = model.getJSONArray("tmpls"); for (int i = 0; i < tmpls.length(); i++) { JSONObject t = tmpls.getJSONObject(i); if (avail.contains(t.getInt("ord"))) { ok.add(t); } } } else { // cloze - generate temporary templates from first for (int ord : avail) { JSONObject t = new JSONObject(model.getJSONArray("tmpls").getString(0)); t.put("ord", ord); ok.add(t); } } } catch (JSONException e) { throw new RuntimeException(e); } return ok; }
/** * Produce a JSONObject by combining a JSONArray of names with the values of * this JSONArray. * * @param names * A JSONArray containing a list of key strings. These will be * paired with the values. * @return A JSONObject, or null if there are no names or if this JSONArray * has no values. * @throws JSONException * If any of the names are null. */ public JSONObject toJSONObject(JSONArray names) throws JSONException { if (names == null || names.length() == 0 || this.length() == 0) { return null; } JSONObject jo = new JSONObject(names.length()); for (int i = 0; i < names.length(); i += 1) { jo.put(names.getString(i), this.opt(i)); } return jo; }
@Override public void onApplicationEvent(ApplicationEvent event) { if (event instanceof ContextStartedEvent) { if (recoveryCheckpoint != null) { JSONObject json = recoveryCheckpoint.loadJson(beanName); try { JSONArray jsonArray = json.getJSONArray("surtPrefixes"); for (int i = 0; i < jsonArray.length(); i++) { surtPrefixes.add(jsonArray.getString(i)); } } catch (JSONException e) { throw new IllegalStateException(e); } } else { readPrefixes(); } } }