多语言展示
当前在线:835今日阅读:6今日分享:31

JQuery利用json读取JavaBean List

JQuery利用json读取JavaBean List
工具/原料

Eclipse软件,jQuery,电脑一台,windows系统

方法/步骤
1

读取JavaBean

2

Java代码:response.setContentType('text/html;charset=utf-8');  PrintWriter out = response.getWriter();  StuBean stu = new StuBean();  stu.setId('s001');  stu.setName('Jahson');  stu.setPassword('123456');  JSONObject jsonObject = JSONObject.fromObject(stu);    out.println(jsonObject.toString());//将json放入页面  System.out.println(jsonObject);  out.flush();  out.close();JQuery代码:$.getJSON('servlet/MoreJson',function(data){     var str=status+'
';      str+='

';          str+='';     str+='';     str+='';     str+='';     str+='';     str+='
'+data.id+''+data.name+''+data.password+'
';     $('#res').append(str);    });

3

多个JavaBeanJava代码:response.setContentType('text/html');  PrintWriter out = response.getWriter();  StuBean stu = new StuBean();  stu.setId('s001');  stu.setName('Jahson');  stu.setPassword('123456');  StuBean stu2 = new StuBean();  stu2.setId('s002');  stu2.setName('Jack');  stu2.setPassword('654321');  List list = new ArrayList();  list.add(stu);  list.add(stu2);  JSONArray jsonArray = JSONArray.fromObject(list);    out.println(jsonArray.toString());  System.out.println(jsonArray);  out.flush();  out.close();JQuery代码:$.post('servlet/MoreJson',{parma:v},function(data,status){     var str='

';     $.each(data,function(index,content){      str+='';      str+='';      str+='';      str+='';      str+='';     });     str+='
'+content.id+''+content.name+''+content.password+'
';     $('#res').append(str);    },'json');

注意事项

List通过each遍历数据信息;单独对象直接调用,多个对象放入LIst中通过each遍历

推荐信息