json对象列表多次打印最后一个元素

时间:2016-04-09 13:38:32

标签: json ajax hibernate spring-mvc

伙计们我有2个表“employee”,它有EMPID,EMPAGE,ADDRESS,SALARY,EMPNAME,department_id(外键参考表“部门”)和“部门”表,其中有department_id,名称......我想要将数据通过JSON对象并将其发送到ajax请求...这是我的代码

public void listDepartment(HttpServletResponse response) throws IOException, JSONException {
    PrintWriter out = response.getWriter();
    JSONObject obj1 = new JSONObject();
    JSONObject obj = new JSONObject();
    List<JSONObject> arrayFinal = new ArrayList<JSONObject>();

    for(int index = 0; index<listDepartment().size(); index++){
        obj1.put("Department ID ",listDepartment().get(index).getDepId());
        obj1.put("Department Name ",listDepartment().get(index).getDepName());

        JSONArray array = new JSONArray();
        for(Employee l : listDepartment().get(index).getEmployees()){
            array.add("Id= "+l.getEmpId()+" Age= "+l.getEmpAge()+" Salary= "+l.getSalary()+" Address= "+l.getEmpAddress());
            obj1.put("Employees ", array);
        }
        arrayFinal.add(index, obj1);
    }   
    System.out.println(arrayFinal);
    obj.put("MyListDep", arrayFinal);
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    out.write(obj.toString());
    out.close();
}

所以arrayFinal打印最后一行5次而不是打印所有5行...任何帮助???

0 个答案:

没有答案