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...

Community
  • 1
  • 1
Tobia
  • 9,165
  • 28
  • 114
  • 219

1 Answers1

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
  });
});
Community
  • 1
  • 1
user219882
  • 15,274
  • 23
  • 93
  • 138
  • 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 May 24 '12 at 05:50
  • @Tobia I'm not quite sure what did you mean by _http request params_ but I updated my answer - see if it fits. – user219882 May 24 '12 at 07:42
  • 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 May 24 '12 at 08:17
  • @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. – user219882 May 24 '12 at 09:07
  • @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. – user219882 May 24 '12 at 09:15
  • I already tried your code but It dosn't work. I get a com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 32. Infact if I write the requeste.getInputStream to a file i see the url ecoded vars as firefug shows to me, not a json string. To reach what you said I should encode object to json before the jquery request. – Tobia May 24 '12 at 12:37
  • This is the request.getInputStream content: data1%5B0%5D%5Bkey%5D=1&data1%5B0%5D%5Bval%5D=2&data1%5B1%5D%5Bkey%5D=3&data1%5B1%5D%5Bval%5D=4 – Tobia May 24 '12 at 12:44
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/11687/discussion-between-tomas-and-tobia) – user219882 May 24 '12 at 12:55
  • This doesn't work, jquery dosn't start the request with processData false – Tobia May 24 '12 at 14:30
  • @Tobia why not? maybe there is another problem... I think you have useless `}` in there... It seems so... Check your JS console for errors – user219882 May 24 '12 at 14:34
  • Plese try my jquery code with "processData:false" you will find what it does not send request. – Tobia May 25 '12 at 06:09