Code example for JSONArray

Methods: getJSONObjectlengthtoString

	//Given a jsonarray that might contain a user or might not, return the id of that user. 
	//if the array is empty, return -1 
	private long searchForID(String json){
		Log.d(TAG, "json = " + json);
		try { 
			JSONArray jarray = new JSONArray(json);
			Log.d(TAG, "jarray = " + jarray.toString());
			Log.d(TAG, "jarray length " + jarray.length());
			//if the input is empty 
			if (jarray.length() < 1) { 
				Log.d(TAG, "json array is empty, return -1");
				return -1; 
				//return null 
			//else, find the id and return that; 
			JSONObject o = jarray.getJSONObject(0);
			String bottle = o.getString("user");
			JSONObject o2 = new JSONObject(bottle);
			return o2.getLong("id");
		} catch (JSONException e) {
Connect your IDE to all the code out there  Get Codota for Java