views:

702

answers:

4

i have a JSON object sent from the browser to the jsp page. how do i receive that object and process it in jsp. do i need any specific parsers? i have used the following piece of code. but it wouldnt work. essentially i should read the contents of the object and print them in the jsp.

<%@page language="java" import="jso.JSONObject"%>

<%
JSONObject inp=request.getParameter("param1");
%>

<% for(int i=0;i<inp.size();i++)
{%>
    <%=inp.getString(i)%>
<%
}
%>
A: 

The svenson JSON library can also be used from JSP.

fforw
A: 

You've got several syntax errors in your example code.

First, request.getParameter returns a String, so setting it to a JSONObject won't work. Secondly, your for loop is incomplete.

I suggest looking into the various JSON libraries available for Java and using one of those.

To help get you started, I'd look at some decoding samples.

Mike Cornell
A: 

In general, you won't be passing JSON within query parameters -- too much quoting needed. Rather, you should POST with JSON as payload (content type 'application/json') or such.

But beyond this, you need a json parser; Json.org lists tons; my favorite is Jackson, which like most alternatives from the page can also be invoked from jsp.

StaxMan
u are right.. the value is posted as you say.. but i m not sure how to receive it and process it in the jsp..
request.getParameter is the right way to get your payload. Then you need to use a json parser to parse the payload into a JSONObject.
Mike Cornell
A: 

You can parse the input string to JSONValue and then cast it to JSONOject as like shown below

  JSONObject inp = (JSONObject) JSONValue.parse(request.getParameter("param1"));
skanduku