본문 바로가기
Spring

javascript xhr // spring // jsonarray Data 전송

by 디찌s 2021. 5. 4.
728x90
반응형
  let formdata = new FormData();
          formdata.enctype='multipart/form-data';
          formdata.method='post';
          formdata.append('data',JSON.stringify(this.bookList));
          formdata.append('deleteCount',this.deleteCount);
          const xhr = new XMLHttpRequest();

  		xhr.open("post", "/draw/data/updateInfoBookData.do", true);

  		xhr.onreadystatechange = function() { //폴백

  			if (xhr.readyState == 4) {

  				if (xhr.status == 200) { //200은 잘넘어왔단 것이다.
  					
  					//callback(JSON.parse(xhr.response),text);
  					

  				} else {

  					alert("요청오류 : " + xhr.status);

  				}

  			}

  		}
  		//post방식은 xhr객체에 데이터를 붙여서 전송
  		xhr.send(formdata); 
    	

this.bookList는

json array 형태로 되어있다.

[{"a":b ,"c":d} , {"a":b ,"c":d} ,{"a":b ,"c":d} ,{"a":b ,"c":d}]

 

 

Controller

	
		String jsonParam = request.getParameter("data").replaceAll(""", "\"");

		JSONParser jsonParse = new JSONParser(); //JSONParse에 json데이터를 넣어 파싱한 다음 JSONObject로 변환한다. 
		Object jsonObj =  jsonParse.parse(jsonParam);
		
		org.json.simple.JSONArray jsonArray = (org.json.simple.JSONArray)jsonObj;

json data를 받으면 replace를 통해 json 형태로 해석해주고

simple jsonArray를 통해 jsonArray 형태로 변경해준다.

 

JSon array 를 

List<Map<String,Object>> info = (ArrayList<Map<String,Object>>)jsonArray;

형태로 변경후

 

 

 

HashMap<String, Object> bookHashMap = new HashMap();
for(int i=0;i<paramMap.size();i++) {
	bookHashMap = new Gson().fromJson(paramMap.get(i).toString(), HashMap.class);
	bookHashMap.put("newPageNum", i+1);

list형식을 for문을 돌린뒤

Gson을 통해 hashMap으로 형변화 시켜준다.

728x90
반응형

댓글