By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,653 Members | 1,483 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,653 IT Pros & Developers. It's quick & easy.

Parsing JSON data in Servlet

P: 5
Hi All,

I am passing json data to servlet as below:
Expand|Select|Wrap|Line Numbers
  1.      Ext.Ajax.request({
  2.         url: CONTEXTPATH + "/ServletURL",
  3.         type: 'post',
  4.         jsonData :{
  5.             record: sample,
  6.             comments: text
  7.                       }
  8.     });
and trying to get that value in servlet but it does not seem to be working. I can see the data in firebug under POST tab and Content-Type is "application/json; charset=UTF-8".

The data that I can see on POST tab is as below:
Expand|Select|Wrap|Line Numbers
  1. {"record":{"key1":"value1","key2":"value2","key3":"value3","key4":"value4","key5":"value5"},"comments":"Testing"}
In servlet I tried to get the value using
Expand|Select|Wrap|Line Numbers
  1. request.getParameter("record")
and
Expand|Select|Wrap|Line Numbers
  1. request.getParameter("comments")
but everything is coming as null.

Please advice what I am doing wrong.

Thanks,
Gaurav
May 14 '13 #1
Share this Question
Share on Google+
1 Reply


10K+
P: 13,264
With jquery the send syntax would be
Expand|Select|Wrap|Line Numbers
  1. dataType : 'json',
  2. data: { jsonData : JSON.stringify(jsondata)},
  3.  
And servlet with
Expand|Select|Wrap|Line Numbers
  1. request.getParameter("jsonData")
Not sure what client library you are using there but maybe try
Expand|Select|Wrap|Line Numbers
  1. request.getParameter("jsonData")
.

You will only get a String representation of the json data not the individual json properties. You will need a json library (e.g Jackson) to parse the json.
May 14 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.