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"); }
JSONObject obj = new JSONObject("{\"name\": \"John\"}"); System.out.println(obj.getString("name")); //John
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") ); }
@Override public List<String> getURLsFromPage(Document doc) { List<String> result = new ArrayList<>(); Elements videos = doc.select("script"); for (Element el : videos) { String json = el.html(); if (json.startsWith("{")) { JSONObject page = new JSONObject(json); result.add(page.getJSONObject("video").getString("contentUrl")); } } return result; }
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; }
String json = "Assuming that here is your JSON response"; try { JSONObject parentObject = new JSONObject(json); JSONObject userDetails = parentObject.getJSONObject("user_details"); //And then read attributes like String name = userDetails.getString("user_name"); String phone = userDetails.getString("user_phone"); String id = userDetails.getString("ref_id"); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); }
@Override public void onResponse(JSONArray response) { try { JSONObject jsonObject = response.getJSONObject(0); firstNameRef.set(jsonObject.getString("firstName")); lastNameRef.set(jsonObject.getString("lastName")); latch.countDown(); } catch (JSONException e) { assertTrue(false); } }
Bundle bundle = getBundleFromIntentOrWhaterver(); JSONObject json = null; try { json = new JSONObject(bundle.getString("json")); String key = json.getString("key"); } catch (JSONException e) { e.printStackTrace(); }
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"); ... }
@Override public CharSequence getPageTitle(int position) { try { return mModel.getJSONArray("tmpls").getJSONObject(position).getString("name"); } catch (JSONException e) { throw new RuntimeException(e); } }
JSONObject ret = getLocationInfo(); JSONObject location; String location_string; try { location = ret.getJSONArray("results").getJSONObject(0); location_string = location.getString("formatted_address"); Log.d("test", "formattted address:" + location_string); } catch (JSONException e1) { e1.printStackTrace(); }
private CommandMode readMode(JSONObject jsonObject) throws JSONException { CommandMode mode = new CommandMode(); mode.name = jsonObject.getString("name"); mode.text = jsonObject.getString("text"); mode.leadOption = jsonObject.getString("lead-option"); mode.options = toStringArray(jsonObject.getJSONArray("options")); return mode; }
public static void main(String[] args) throws Exception { String jsonStr = "{ \"dataArray\": [{ \"A\": \"a\", \"B\": \"b\", \"C\": \"c\" }, { \"A\": \"a1\", \"B\": \"b2\", \"C\": \"c3\" }] }"; JSONObject jsonObj = new JSONObject(jsonStr); JSONArray c = jsonObj.getJSONArray("dataArray"); for (int i = 0 ; i < c.length(); i++) { JSONObject obj = c.getJSONObject(i); String A = obj.getString("A"); String B = obj.getString("B"); String C = obj.getString("C"); System.out.println(A + " " + B + " " + C); } }
Object response = ((JavascriptExecutor) driver).executeAsyncScript( "var callback = arguments[arguments.length - 1];" + "var xhr = new XMLHttpRequest();" + "xhr.open('GET', '/resource/data.json', true);" + "xhr.onreadystatechange = function() {" + " if (xhr.readyState == 4) {" + " callback(xhr.responseText);" + " }" + "}" + "xhr.send();"); JSONObject json = new JSONObject((String) response); assertEquals("cheese", json.getString("food"));
JSONArray jsonarray = new JSONArray(jsonStr); for (int i = 0; i < jsonarray.length(); i++) { JSONObject jsonobject = jsonarray.getJSONObject(i); String name = jsonobject.getString("name"); String url = jsonobject.getString("url"); }
public static Message toObject(String jsonStr) { Message m = new Message(); try { JSONObject jsonObject = new JSONObject(jsonStr); m.setHandlerName(jsonObject.has(HANDLER_NAME_STR) ? jsonObject.getString(HANDLER_NAME_STR):null); m.setCallbackId(jsonObject.has(CALLBACK_ID_STR) ? jsonObject.getString(CALLBACK_ID_STR):null); m.setResponseData(jsonObject.has(RESPONSE_DATA_STR) ? jsonObject.getString(RESPONSE_DATA_STR):null); m.setResponseId(jsonObject.has(RESPONSE_ID_STR) ? jsonObject.getString(RESPONSE_ID_STR):null); m.setData(jsonObject.has(DATA_STR) ? jsonObject.getString(DATA_STR):null); return m; } catch (JSONException e) { e.printStackTrace(); } return m; }
@Override public void onNext(JSONArray response) { try { JSONObject jsonObject = response.getJSONObject(0); firstNameRef.set(jsonObject.getString("firstName")); lastNameRef.set(jsonObject.getString("lastName")); latch.countDown(); } catch (JSONException e) { assertTrue(false); } }
try { JSONObject jsonObject = new JSONObject(theJsonString); Iterator keys = jsonObject.keys(); Map<String, String> map = new HashMap<String, String>(); while (keys.hasNext()) { String key = (String) keys.next(); map.put(key, jsonObject.getString(key)); } System.out.println(map);// this map will contain your json stuff } catch (JSONException e) { e.printStackTrace(); }
public String getAfter(JSONObject json) { try { return json.getJSONObject("entry_data").getJSONArray("ProfilePage").getJSONObject(0) .getJSONObject("graphql").getJSONObject("user") .getJSONObject("edge_owner_to_timeline_media").getJSONObject("page_info").getString("end_cursor"); } catch (JSONException e) { // This is here so that when the user rips the last page they don't get a "end_cursor not a string" error try { return json.getJSONObject("data").getJSONObject("user") .getJSONObject("edge_owner_to_timeline_media").getJSONObject("page_info").getString("end_cursor"); } catch (JSONException t) { return ""; } } }