記錄

Javascript) 클라이언트에서 보낸 배열을 서버에서 받기 본문

IT_Fundamental/etc

Javascript) 클라이언트에서 보낸 배열을 서버에서 받기

surhommejk 2018. 5. 2. 11:44


Ex 1)

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
</script>
    <script type="text/javascript">
$(document).ready(function() {
$("#sdata").click(function() {
var string = ["abc","def"];
var vParam = "test";
$.ajax({
url: "DataArray.jsp",
type: "GET",
data: {"string":string}, //jsp?string=abc&string=def&string=aaa
success: function(data) {
var as = eval(data);
alert("data:"+as[0]+"/"+as[1]);
},
error: function(msg, error) {
alert(error);
}
});
});// end .click()
}); //end jQuery ready
</script>

</head>
<body>
    <input type="button" value="서버 전송" id="sdata">
</body>
</html>


//String[] aStr = request.getParameterValues("string"); 일반적인 동기형태 (checkbox)
String[] aStr = request.getParameterValues("string[]");





Ex 2)


<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
</script>
    <script type="text/javascript">
$(document).ready(function() {
$("#sdata").click(function(){
     var jsondata = {"name":"kglim","age":100};
     var stringJson = JSON.stringify(jsondata);
     alert(stringJson);
     $.ajax({
url: "DataArray2.jsp",
type: "GET",
data: {"json":stringJson},
//data:{"name":"kglim","age":100}; ?name=kglim&age=100
success: function(data) {
alert(data);
},
error: function(msg, error) {
alert(error);
}
});
    
});
});   
/* $.ajax({
url: "DataArray2.jsp",
type: "GET",
data: {"jsonArrayData":[{"idx":1,"date":{"title":"책","id":"abcd"}}]},
//data: {"jsonArrayData":"aaaaaaaaaaaaaaaa"},
success: function(data) {
alert(data);
},
error: function(msg, error) {
alert(error);
}
});
});
}); */
</script>

</head>
<body>
    <input type="button" value="서버 전송" id="sdata">
</body>
</html>


String data = request.getParameter("json");
//{"name":"kglim","age":100}
JSONParser json = new JSONParser();
JSONObject jsonobj = (JSONObject)json.parse(data);
//System.out.println(jsonobj);
System.out.println(jsonobj.get("name"));
System.out.println(jsonobj.get("age"));






Ex 3)

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
</script>
    <script type="text/javascript">
$(document).ready(function() {
$("#sdata").click(function(){
    
var jsondata = {
         "books": [
         {
         "genre": "소설",
         "price": "100",
         "name": "사람은 무엇으로 사는가?",
         "writer": "톨스토이",
         "publisher": "톨스토이 출판사"
         },
         {
         "genre": "소설",
         "price": "300",
         "name": "홍길동전",
         "writer": "허균",
         "publisher": "허균 출판사"
         },
         {
         "genre": "소설",
         "price": "900",
         "name": "레미제라블",
         "writer": "빅토르 위고",
         "publisher": "빅토르 위고 출판사"
         }
         ],
         "persons": [
         {
         "nickname": "남궁민수",
         "age": "25",
         "name": "송강호",
         "gender": "남자"
         },
         {
         "nickname": "예니콜",
         "age": "21",
         "name": "전지현",
         "gender": "여자"
         }
         ]
        }
    
    var stringJson = JSON.stringify(jsondata);
     alert(stringJson);
     $.ajax({
url: "DataArray3.jsp",
type: "GET",
data: {"json":stringJson},
success: function(data) {
alert(data);
},
error: function(msg, error) {
alert(error);
}
});
    
});
});   
/* $.ajax({
url: "DataArray2.jsp",
type: "GET",
data: {"jsonArrayData":[{"idx":1,"date":{"title":"책","id":"abcd"}}]},
//data: {"jsonArrayData":"aaaaaaaaaaaaaaaa"},
success: function(data) {
alert(data);
},
error: function(msg, error) {
alert(error);
}
});
});
}); */
</script>

</head>
<body>
    <input type="button" value="서버 전송" id="sdata">
</body>
</html>


String data = request.getParameter("json");
System.out.println(data);
JSONParser json = new JSONParser();
JSONObject jsonobj = (JSONObject)json.parse(data);
//System.out.println(jsonobj);
JSONArray bookInfoArray = (JSONArray)jsonobj.get("books");
for(int i=0; i<bookInfoArray.size(); i++){
     //배열 안에 있는것도 JSON형식 이기 때문에 JSON Object 로 추출
     JSONObject bookObject = (JSONObject) bookInfoArray.get(i);
    System.out.println("bookInfo: name==>"+bookObject.get("name"));
     System.out.println("bookInfo: writer==>"+bookObject.get("writer"));
     System.out.println("bookInfo: price==>"+bookObject.get("price"));
     System.out.println("bookInfo: genre==>"+bookObject.get("genre"));
     System.out.println("bookInfo: publisher==>"+bookObject.get("publisher"));
     }


'IT_Fundamental > etc' 카테고리의 다른 글

개발자 로드맵  (0) 2019.09.07
script에서 request에 parameter 설정  (0) 2018.04.13
Model1 vs Model2 (간단히)  (0) 2018.03.30
Setting  (0) 2018.03.27
UML(Class Diagram)  (0) 2018.02.12
Comments