Reading json object into jsp

I have a JSON object passed to a jsp page. it is passed as a string. now I have to parse this string and get the values ​​that are passed through the JSON object. so that i can print the values ​​in the same jsp.

+1


source to share


3 answers


There are many resources out there including libraries and plugins for various technologies / frameworks at json.org .



+1


source


With tons of JSON parsers, it boils down to how you want to deal with data in JSON. My personal favorite is the Jackson batch , but many others work well for simple cases too , including the "reference implementation" (aka JSON.org parser ).



(I assume you need a Java parser given the jsp link)

+1


source


My preferred solution to this problem is to use a JSON parser which provides output that implements the java.util.Map and java.util.List interface. This makes it easy to parse the JSON structure in the JSP expression language.

Here's an example using JSON4J with Apache Wink. The sample imports JSON data from a URL, parses it into Java script, and walks through the resulting structure.

<c:import var="dataJson" url="http://localhost/request.json"/>
<% 
String json = (String)pageContext.getAttribute("dataJson");
pageContext.setAttribute("parsedJSON", org.apache.commons.json.JSON.parse(json));
%>
Fetch the name of the node at index 1 : ${parsedJSON.node[1].name}

      

To keep this clean, it would be preferable to create a JSTL tag for parsing and avoid java script.

<c:import var="dataJson" url="http://localhost/request.json"/>
<json:parse json="${dataJson}" var="parsedJSON" />
Fetch the name of the node at index 1 : ${parsedJSON.node[1].name}

      

0


source







All Articles