2016-08-27 2 views
0

JSON 개체를 자바 스크립트에서 java 서버let으로 게시하고 싶습니다. 자바 코드 다음자바 서블릿 JSON 데이터 구문 분석

var a = {}; 
function SaveInfo(c) 
{ 
    document.getElementById("saveButton").className = "savebutton"; 
    document.getElementById("cancelButton").className = "cancebutton"; 
    var z= $(c).parent().parent().attr('id'); 
    a[z] = c.value; 
} 
$("#saveButton").click(function(e){ 
    if( document.getElementById("saveButton").className=="savebutton") 
     $.ajax({ 
      url: "../../update", 
      type: 'POST', 
      dataType: 'json', 
      data: JSON.stringify(a), 
      contentType: 'application/json', 
      mimeType: 'application/json', 
      success: function (data) { 
       consol.log(data); 
      }, 
      error:function(data,status,er) { 
       alert("error: "+data+" status: "+status+" er:"+er); 
      } 
     }); 
}); 

과 :

public void doPost(HttpServletRequest req, HttpServletResponse res){ 
    JSONObject jObj; 
    jObj = new JSONObject(req.getParameter("mydata")); // this parses the json 
    Iterator it = jObj.keys(); 

    while(it.hasNext()) 
    { 
     String key = (String)it.next(); // get key 
     Object o = jObj.get(key); // get value 
     // store in session 
    } 
} 

JSON이 성공적으로 수신 나는 다음과 같은 자바 스크립트 코드를 사용했다. 나는 자바 스크립트를 통해 전달 된 두 값과 키를 읽을 수

jObj = new JSONObject(req.getParameter("mydata")); 

: 문제에 대한 생성자가없는 것입니다.

답변

0

당신은 단순히 양식을 사용할 수 있습니다를, 단순히 좋아하는 구문 분석 :

Map<String, String[]> map = request.getParameterMap(); 

    for(Entry<String, String[]> e : map.entrySet()){ 
     response.getWriter().write(e.getKey() + ":" + e.getValue()[0] + "\n"); 
    } 

사용 JSON을 사용하면 객체 또는 복잡한 데이터를 보낼 때 배열 등.

희망은 당신의 작업을 완료합니다.

+0

감사합니다. – Shomit

0

다음보십시오 : 당신의 목적을 위해

JSONParser parser = new JSONParser(); 
JSONObject json = (JSONObject) parser.parse(req.getParameter("mydata").toString());