天天看點

Ajax 擷取Map集合

Java代碼:

Student stu1=new Student();
            stu1.setName("Ajax1");
            stu1.setSex("nan");
            stu1.setAge(22);
            Student stu2=new Student();
            stu2.setName("Ajax2");
            stu2.setSex("nv");
            stu2.setAge(21);
 Map map=new HashMap();
 map.put("stu1", stu1);
 map.put("stu2", stu2);
JSONObject jsonobject=JSONObject.fromObject(map);
response.getWriter().print(jsonobject);
           

Ajax代碼:

$.ajax({
                type : "POST",
                url : "ajaxtest",
                async : false,
                data : {num:num},
                success : function(data) {

                    var Data=data;
                  for(var key in Data) { 
                       alert("鍵:" + key + ",值 :{姓名:"+ Data[key].name+", 年齡:"+Data[key].age+"}");  
                    }

                },
                dataType : "json"
            }); 
           

繼續閱讀