相关疑难解决方法(0)

在servlet中读取JSON字符串

我将jQuery AJAX POST发布到servlet,数据采用JSON String的形式.它成功发布但在Servlet端我需要将这些key-val对读入Session对象并存储它们.我尝试使用JSONObject类,但我无法得到它.

下面是代码片段

$(function(){
   $.ajax(
   {
      data: mydata,   //mydata={"name":"abc","age":"21"}
      method:POST,
      url: ../MyServlet,
      success: function(response){alert(response);
   }
});
Run Code Online (Sandbox Code Playgroud)

在Servlet方面

public doPost(HTTPServletRequest req, HTTPServletResponse res)
{
     HTTPSession session = new Session(false);
     JSONObject jObj    = new JSONObject();
     JSONObject newObj = jObj.getJSONObject(request.getParameter("mydata"));
     Enumeration eNames = newObj.keys(); //gets all the keys

     while(eNames.hasNextElement())
     {
         // Here I need to retrieve the values of the JSON string
         // and add it to the session
     }
}
Run Code Online (Sandbox Code Playgroud)

java json servlets

20
推荐指数
2
解决办法
9万
查看次数

标签 统计

java ×1

json ×1

servlets ×1