Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed to send json data from js to java
    primarykey
    data
    text
    <p>I have a JSON string in my javascript, I need to send this to a java class. Can I use a plain java class or is it necessary to use a servlet?</p> <p>Also, How can I parse the JSON data?</p> <p>My JSON data is of the following format:</p> <pre><code> var details= { "CustomerDetails": [{'name':'Amy','age':'23'}, {'name':'Amj','age':'25'},{'name':'Amg','age':'27'}]}; </code></pre> <p>I send the json string like this:</p> <pre><code> var jsonText = JSON.stringify(details); $.post('ServletClass', jsonText); </code></pre> <p><strong>WEb.xml:</strong></p> <pre><code>&lt;servlet&gt; &lt;servlet-name&gt;ServletClass&lt;/servlet-name&gt; &lt;display-name&gt;ServletClass&lt;/display-name&gt; &lt;servlet-class&gt;java.files.ServletClass&lt;/servlet-class&gt; &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;ServletClass&lt;/servlet-name&gt; &lt;url-pattern&gt;/ServletClass&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; </code></pre> <p><strong>On my servlet side:</strong> I try to do a system.out.println, but get nothing, is the transfer even happening properly</p> <pre><code>package XXXX; import javax.servlet.*; import javax.servlet.http.*; import java.io.IOException; import java.files.JSONObject; import java.files.JSONSerializer; public class ServletClass extends HttpServlet { public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { } public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException { // doGet(request, response); //JSONArray arrayObj = new JSONArray(); JSONObject jsonObj = new JSONObject(jsonText); JSONArray the_ json_array = jsonObj.getJSONArray("CustomerDetails"); System.out.println("FINALLY HERE"); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload