본문 바로가기

개발하자/ajax&json

json을 이용하여 list보내기

a.jsp

============================

<%@ page language="java" contentType="text/html; charset=EUC-KR"
 pageEncoding="EUC-KR"%>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script>
$(document).ready(function(){
 var str='{"list":[{"id":"id1","pw":"pw1"},{"id":"id2","pw":"pw2"}]}';
 $.ajax({
 url:"b.jsp",
 data:{"aa":str},
 cache:false,
 success:function(data){
alert(data);
 }
 });
});
</script>
<body>
</body>
</html>

 

b.jsp

=====================================

<%@ page language="java" contentType="text/html; charset=EUC-KR"
 pageEncoding="EUC-KR"%>
<%@ page import="java.util.*" %>
<%@ page import="kr.co.seoulit.member.dao.*" %>
<%@ page import="kr.co.seoulit.member.to.*" %>
<%@ page import="net.sf.json.*"%>
<%
 String str=request.getParameter("aa");
 JSONObject jsonObject=JSONObject.fromObject(str);
 JSONArray jsonArray=jsonObject.getJSONArray("list");
 ArrayList<MemberBean> list=new ArrayList<MemberBean>();
 for(Object obj:jsonArray){
JSONObject json=(JSONObject)obj;
MemberBean bean=(MemberBean)JSONObject.toBean(json,MemberBean.class);
list.add(bean);
 }
 out.println(list.get(0).getId());
 out.println(list.get(0).getPw());
 out.println(list.get(1).getId());
 out.println(list.get(1).getPw());
%>