user5083862 user5083862 - 4 years ago 149
JSON Question

Index 2 out of range JSON

As our activity, we are given this kind of

json
and we are asked to display it in
listview
in android. However, the error says that index 2 out of range. I am newbie to android programming please help me out.

Json

{"aaData":[[
8590614,
" Jefferson",
"Miranda",
"N/A",
"Regular",
"N/A",
"N/A",
"N/A"
],
[
8590615,
" Jeff",
"Mir",
"/A",
"Probationary",
"/A",
"/A",
"/A"
]]

}


Code:

if (jsonStr != null) {
try {

JSONObject jsonObj = new JSONObject(jsonStr);
employees = jsonObj.getJSONArray("aaData");
for (int i = 0; i < employees.length(); i++) {

String id = employees.getString(0);
String firstname = employees.getString(1);
String lastname =employees.getString(2);
String group = employees.getString(3);
String status = employees.getString(4);
String paytype = employees.getString(5);
String workcode = employees.getString(6);
String shiftcode = employees.getString(7);
// tmp hashmap for single contact

HashMap<String, String> employee = new HashMap<String, String>();

// adding each child node to HashMap key => value
employee.put("id", id);
employee.put("first_name", firstname);
employee.put("last_name", lastname);
employee.put("group", group);
employee.put("status",status);
employee.put("paytype", paytype);
employee.put("workcode", workcode);
employee.put("shiftcode", shiftcode);

// adding employee to employee list

employeeList.add(employee);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}


Logs

07-06 13:05:54.758 22573-22589/**********_sample D/Response:﹕ > {"aaData":[[8590614," Jefferson","Miranda","N\/A","Regular","N\/A","N\/A","N\/A"],[8590615," Jeff","Miranda","N\/A","Probationary","N\/A","N\/A","N\/A"]]}
07-06 13:05:54.761 22573-22589/**********_sample W/System.err﹕ org.json.JSONException: Index 2 out of range [0..2)
07-06 13:05:54.764 22573-22589/**********_sample W/System.err﹕ at org.json.JSONArray.get(JSONArray.java:293)
07-06 13:05:54.764 22573-22589/**********_sample W/System.err﹕ at org.json.JSONArray.getString(JSONArray.java:462)
07-06 13:05:54.764 22573-22589/**********_sample W/System.err﹕ at **********_sample.MainActivity$GetEmployees.doInBackground(MainActivity.java:116)
07-06 13:05:54.764 22573-22589/**********_sample W/System.err﹕ at com.example.piasi_pc1.api_sample.MainActivity$GetEmployees.doInBackground(MainActivity.java:84)
07-06 13:05:54.764 22573-22589/**********_sample W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:292)
07-06 13:05:54.764 22573-22589/**********_sample W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-06 13:05:54.764 22573-22589/**********_sample W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
07-06 13:05:54.764 22573-22589/**********_sample W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
07-06 13:05:54.764 22573-22589/**********_sample W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
07-06 13:05:54.764 22573-22589/**********_sample W/System.err﹕ at java.lang.Thread.run(Thread.java:818)
07-06 13:05:55.037 1242-1265/system_process I/ActivityManager﹕ Displayed com.example.piasi_pc1.api_sample/.MainActivity: +852ms
07-06 13:05:55.049 1242-1390/system_process I/WindowManager﹕ Switching to real app window: Window{313018ce u0 **********_sample/**********_sample.MainActivity}
07-06 13:05:55.259 1316-1316/com.android.systemui W/ResourceType﹕ No package identifier when getting value for resource number 0x00000000
07-06 13:05:55.260 1316-1316/com.android.systemui W/PackageManager﹕ Failure retrieving resources for **********_sample_1: Resource ID #0x0
07-06 13:05:58.708 1242-1299/system_process D/TaskPersister﹕ removeObsoleteFile: deleting file=627_task.xml
07-06 13:05:58.708 1242-1299/system_process D/TaskPersister﹕ removeObsoleteFile: deleting file=627_task_thumbnail.png
07-06 13:11:46.047 1242-1259/system_process I/UsageStatsService﹕ User[0] Flushing usage stats to disk

Answer Source

Change in you for loop like as below:

for (int i = 0; i < employees.length(); i++) {

 JSONArray jsonArray = employees.getJSONArray(i)
 String id = jsonArray.getString(0);
 String firstname = jsonArray.getString(1);
 String lastname = jsonArray.getString(2);
 String group = jsonArray.getString(3);
 String status = jsonArray.getString(4);
 String paytype = jsonArray.getString(5);
 String workcode = employees.getString(6);
 String shiftcode = employees.getString(7);

 HashMap<String, String> employee = new HashMap<String, String>();
 // adding each child node to HashMap key => value
 employee.put("id", id);
 employee.put("first_name", firstname);
 employee.put("last_name", lastname);
 employee.put("group", group);
 employee.put("status",status);
 employee.put("paytype", paytype);
 employee.put("workcode", workcode);
 employee.put("shiftcode", shiftcode);

  // adding employee to employee list
  employeeList.add(employee);
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download