索引2超出范围JSON

时间:2015-07-06 05:09:36

标签: android json

作为我们的活动,我们会收到此类json,我们会被要求在机器人listview中显示它。但是,错误表示索引2超出范围。我是android编程的新手,请帮帮我。

的Json

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

}

代码:

 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");
        }

日志

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  

2 个答案:

答案 0 :(得分:1)

在for循环中创建另一个数组并尝试从该数组中获取数据

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

JSONArray jA = employees.getJSONArray(i)

String id = jA.getString(0);

String firstname = jA.getString(1);

String lastname =jA.getString(2);

String group = jA.getString(3);

String status = jA.getString(4);

String paytype = jA.getString(5);

String workcode = jA.getString(6);

String shiftcode = jA.getString(7);

}

答案 1 :(得分:0)

改变你的循环,如下所示:

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);
}