2

my data:

var test = {cars : []};

var cars = []
cars.push({
    "name" : "Ford",
    "year" : "2000"
});

    cars.push({
    "name" : "Audi",
    "year" : "2002"
});

test.cars = cars;   

var json = JSON.stringify(test);

$.get('/myservlet/', json, function(data) { // also tried .getJSON , .post
            alert('Success');                               
})

In Java I get the "json" variable as parameter key, but no value.

public void doPost(...) // TRIED BOTH
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {           
    for(Object a : req.getParameterMap().keySet()) {
        System.out.println(a + " - " + req.getParameter((String)a));
    }
//prints out
{"cars":[{"name":"Ford","year":"30"},{"name":"Audi","year":"2002"}]} - 

This is unusable result, because the key is always changing, and is ridiculous to for-loop the params everytime. I need to have a specific key : req.getParameter("cars")

Jaanus
  • 16,161
  • 49
  • 147
  • 202

3 Answers3

2

Change it to:

$.get('/myservlet/', 'cars='+ json, function(data) { // also tried .getJSON , .post
        alert('Success');                   
Janoz
  • 953
  • 4
  • 9
  • This way you still end up with a stringified JSON object as request parameter value in the server side which you'd have to manually parse with a JSON parser, which doesn't make the service exactly reuseable. – BalusC Nov 26 '12 at 15:15
0

You shouldn't have stringified the JSON at all. The whole JSON object test is supposed to be treated as the request parameter map. Just pass the JSON object unmodified.

$.get('/myservlet/', test, function(data) {
    // ...
});

This way it's available in the servlet as follows:

for (int i = 0; i < Integer.MAX_VALUE; i++) {
    String name = request.getParameter("cars[" + i + "][name]");
    if (name == null) break;
    String year = request.getParameter("cars[" + i + "][year]");
    // ...
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
-1

Update Your question could be possible duplicate of READ JSON String in servlet

Previous answer

I assume you are trying to post JSON to the servlet, if thats the case read on.

You would have to check for request body instead of request parameter. Something like

BufferedReader buff = req.getReader();

Check if this works for you

public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
  BufferedReader buff = req.getReader();
  char[] buf = new char[4 * 1024]; // 4 KB char buffer
  int len;
  while ((len = reader.read(buf, 0, buf.length)) != -1) {
   out.write(buf, 0, len);
  }
}

Note that I have checked the above code for syntax errors. I hope you get the idea.

Community
  • 1
  • 1
ch4nd4n
  • 4,110
  • 2
  • 21
  • 43
  • Your "previous answer" makes no utter sense. The parameters are in case of a HTTP GET request not sent in request body at all, but instead in request URL. With only posting a link to the dupe you're in no way explaining OP's concrete problem. – BalusC Nov 26 '12 at 15:14
  • @BalusC nor does your comment make any sense and falls under same category as my answer. I have clearly said that "I assume you are trying to POST". I was unclear what the OP is asking. In code comments he has mentioned that he has tried both POST and GET. The mistake in my code snippet is that I should have it doPost instead of doGet. Thanks for your bright comment. – ch4nd4n Nov 27 '12 at 04:38