如何将列表放在json对象中并在客户端迭代?

时间:2014-03-20 12:57:35

标签: java jquery json

通过jquery ajax我正在呼叫服务器端。在服务器端,我有两个列表,我将两个列表放入地图中,映射到Json Obect但它没有设置。

如何通过使用jquery创建两个选择框来迭代这两个列表我使用了以下代码。

服务器端

 List<DepartmentMaster> deplist = HrDao.getDepartments();
        System.out.println("deplistdeplist" + deplist.size());
        if (!deplist.isEmpty()) {
            request.setAttribute("deplist", deplist);
        }
        List<EmployeeEntity> reportinglist = HrDao.getReportinTo();
        System.out.println("listsize" + reportinglist.size());
        if (!reportinglist.isEmpty()) {
            request.setAttribute("reportinglist", reportinglist);
        }
        Map<String, List> map = new HashMap<String, List>();
        map.put("deplist", deplist);
        map.put("reportinglist", reportinglist);
        System.out.println("map" + map);
        try{
        JSONObject jsonobj = new JSONObject();
        jsonobj.accumulateAll(map);
        System.out.println("jsonobj"+jsonobj);
        response.setContentType("application/json");
        response.getWriter().print(jsonobj.toString());

客户端

 $("#departmentreporting").click(function() {
        $.ajax({type: 'POST', url: 'getdeparmentreporting.do?', success: function(result) {
                var obj = JSON.parse(result);
                alert(obj);
                if (result !==null) {

                  $(this).closest('tr').prev('tr').after()
                }
            }
        });
    });

1 个答案:

答案 0 :(得分:0)

如果从服务器返回有效的json,则在客户端

     for (var i = 0; i < result.length; i++) {
       //pick the fields of result here to do whatever you want
     }