Parsing JSON data in Servlet

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gjain123
    New Member
    • Nov 2008
    • 5

    Parsing JSON data in Servlet

    Hi All,

    I am passing json data to servlet as below:
    Code:
    	 Ext.Ajax.request({
    	    url: CONTEXTPATH + "/ServletURL",
    	    type: 'post',
    	    jsonData :{
    			record: sample,
    			comments: text
                          }
    	});
    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 "applicatio n/json; charset=UTF-8".

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

    Please advice what I am doing wrong.

    Thanks,
    Gaurav
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    With jquery the send syntax would be
    Code:
    dataType : 'json',
    data: { jsonData : JSON.stringify(jsondata)},
    And servlet with
    Code:
    request.getParameter("jsonData")
    Not sure what client library you are using there but maybe try
    Code:
    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.

    Comment

    Working...