2

EDIT Maybe this is better explanation (sorry, I know javascript better than English... and I don't know very well javascript :-)

I need to map a complex javascript object sent with jquery $.ajax in a java bean by my servlet.

This is a real example done with jQuery and sniffed with Firebug. This is the js code:

$("#test").click(function(){
    $.ajax({
        url: "/server", 
        data: { "data1": [{key:1, val:2},{key:3, val:4}] }, 
        type: "post",               
        }
    });
});

As you can see I'm sending a complex object inside $.ajax data, not a simple map. If I check how the http request is sent with firebug sniffer i get this POST params:

Parameters application/x-www-form-urlencoded
data1[0][key]   1
data1[0][val]   2
data1[1][key]   3
data1[1][val]   4

This is how jquery convert a complex object into a simple map. I would like to get this object in a java bean. Thanks.

EDIT2 This: Java convert JSONObject to URL parameter seems to be exactly the opposite of my demand. But it has no answer...

1 Answer 1

2

This is JSON and you can use Gson library for mapping it to POJO. See this tutorial

Reading a HttpServletRequest

Gson gson = new Gson();
SomeClass someClass = gson.fromJson(
        new InputStreamReader(httpServletRequest.getInputStream()), SomeClass.class);

EDIT

You may want to look at this topic which should help you with mapping your JSON to POJO (or Java beans as you say).

EDIT 2

Try to add processData parameter and set it to false. This should prevent transforming of the given JSON into a query string.

$("#test").click(function(){
  $.ajax({
      url: "/server", 
      data: { "data1": [{key:1, val:2},{key:3, val:4}] }, 
      type: "post", 
      processData: false
  });
});
Sign up to request clarification or add additional context in comments.

11 Comments

I already use Gson, but i think it can decode from a json string, do you mean that it can catch object also from http request params?
@Tobia I'm not quite sure what did you mean by http request params but I updated my answer - see if it fits.
I tried to explain better editing my question, in your example seems that my httpServletRequest has a json string inside a not a get/post params
@Tobia That Firebug sniffer does not show how the HTTP request really looks like. The thing is that each requests consists of headers and data. And your complex object is converted to JSON and sent in the data part. If you want to restore that object, you have to (not the only option) create POJOs and use the InputStreamReader to convert JSON from data part into a POJO.
@Tobia To make it clear - what you see in the Firebug sniffer is parsed and formatted HTTP request. In Java servlet you get the exact string as you have after data: in your JavaScript.
|

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.