0

I am tring to send a json string from js to servlet. I use ajax jquery to post to the servlet.

I can see the servlet post getting called but an exception occurs when I retrieve the json string in java, what am I missing

Javascript side:

var jsonText = JSON.stringify(SomeString);

    $.ajax({
        dataType: 'json',
        url: fullpath,
        type: 'POST',
        data: jsonText 
  });

Java side:

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;

import com.json.JSONException;
import com.json.JSONObject;
import com.json.JSONArray;

In post

    response.setContentType("application/json");    
JSONObject jObj = new JSONObject(request.getParameter("jsonText"));//Null pointer exception

Error:

 java.lang.NullPointerException
        at java.io.StringReader.<init>(StringReader.java:33)
        at com.json.JSONTokener.<init>(JSONTokener.java:84)
        at com.json.JSONObject.<init>(JSONObject.java:314)

JSON string:

{ "CustomerDetails": [{'name':'Amy','age':'23'},{'name':'Amj','age':'25'},{'name':'Amg','age':'27'}]}
7
  • What error do you get? And what Java code are you using? Commented Aug 1, 2012 at 15:58
  • Can you give an example of the JSON being posted? Commented Aug 1, 2012 at 16:01
  • You get null pointer at response.setContentType("application/json"); OR JSONObject jObj = new JSONObject(request.getParameter("jsonText")); Commented Aug 1, 2012 at 16:01
  • sorry at request.get parameter Commented Aug 1, 2012 at 16:02
  • 2
    When posting JSON, there would not be a parameter named jsonText. Just because that is what the JSON body is called in javascript, does not mean that there is a parameter named that, unless you specifically created a query string with that as one of the keys. Commented Aug 1, 2012 at 16:04

2 Answers 2

2

In your POST, there is no jsonText parameter. You're just posting the JSON string as the post body.

Try to send it like this:

$.ajax({
    dataType: 'json',
    url: fullpath,
    type: 'POST',
    data: {jsonText: jsonText}
});
Sign up to request clarification or add additional context in comments.

5 Comments

With this request.getParameter("jsonText") should work right, let me try
shouldn't it be data: {'jsonText': jsonText}?
Yes, now your Java code should work. Also, you don't need to quote the keys in a JavaScript object.
For the sake of completeness: if you can't alter what the client sends, you can get the POST data by reading it out as a String from request.getInputStream() and then passing that String into the JSONObject constructor. However, Rocket's solution is much simpler, as long as you can alter the client behavior.
@apsillers: Yes, that works too. I usually don't read from the raw post body unless I have to. :-P
0

This suggests there is no POST parameter called jsonText

1 Comment

This should probably be a comment, not an answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.