I am posting a jQuery AJAX POST in a servlet and the data is in the form of a JSON string. It is being successfully posted but on the servlet side I need to read and store these key-val pairs in a session object. I tried using the JSONObject class but I can not understand
here the code snippet
$ (function () {$ .ajax ({data: MyData, // MyData = {"name": "abc", "age": "21"} method: post, url: ../MyServlet, success: function (response) {warning (response);}}); Public doPost (HTTPServletRequest request, HTTPServletResponse resolution) {HttpSession session = new session (wrong); JSONObject jObj = new JSONObject (); servlet side JSONObject newObj = jObj.getJSONObject (request.getParameter ("MyData"); Calculation eNames = newObj.keys (); // All keys While (eNames.hasNextElement ()) // Here I need to retrieve the values of JSON string // and add it to the session}} < P>
You're not actually parsing json
JSONObject jObj = new JSONObject (request.getParameter ("MyData")). // This json iterator parses jObj.keys () =; // gets all the keys while (this hasNext ()) {string key = it.next (); // key object o = jObj.get (key); // Receive Price Session. Input value (key, o); // store in session}
Comments
Post a Comment