Home > Java > JSON in JAVA

JSON in JAVA

JSON is being widely used in Web Technology for data transfer in JavaScript. But with AJAX coming into picture JSON has become the most popular tool for sending data from remote page to the calling page. Well there are different packages available for implementing JSON in Java. Here i am using one of the package to show how we can use the JSON package in Java.
The JSON Package that i will be using is from: JAVA JSON Package
This article will give a small glimpse of how we can send large data from JSP/Servlet to client Page using JSON and AJAX.

The JSON Package used here can send data in two different format JSONArray and JSONObject. Tha main difference between JSONArray and JSONObject is that JSONArray uses ArrayList to store the data whereas JSONObject uses HashMap to store the data.

Java Servlet implementing JSON

import org.json.simple.JSONObject;
public class json extends HttpServlet {
	public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
	JSONObject array= new JSONObject();
	array.put("data1","Hello");
	array.put("data2","World");
	array.put("data3","Good");
	array.put("data4","Morning");
	}		
	public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
		doPost();
	}
}

HTML Page reading JSON value:

<html>
<head>
<script language="Javascript">
   function getHTTPObject() {
   	var xmlhttp = false;
	try {
	      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e) {
	    try {
	         xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	    }
	    catch (e) {
	       try {
	            xmlhttp = new XMLHttpRequest();
    	       }
    	       catch (e) {
	            xmlhttp = false;
    	       }
    	}
		}	
		return xmlhttp;
   }
 
   var xmlhttp = new getHTTPObject();	//Initialize New Ajax Object
 
   function Connect2RemoteSite(url,funcname,parameter)  {
      try {
	  if(xmlhttp) {
	       xmlhttp.open("POST", url, true);	//URL will be the servlet page
               xmlhttp.onreadystatechange = funcname;
               xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	       xmlhttp.setRequestHeader("Content-Length", parameter.length);
               xmlhttp.send(parameter);
	  }
         else {
              document.getElementById("rotateimage").style.display = "none";
	      alert("Your Browser dosen't support Ajax");
        }
    }
    catch(e) {
	alert("Some Unknown Error Occured. Please Try Again" + e);
    }    
 
 }
 
  function AjaxResponse() {
	if(xmlhttp.readyState == 4) {
   	   if(xmlhttp.status == 200) {
    		//servletdetail holds the JSONArray Details
    		servletdetail = eval(xmlhttp.responseText);
    	   }
    	}
    }			
</script>	
</head>
<body>
</body>
</html>

Your email:

 


Here i have given the JSON implementation with AJAX. JSON object is getting created in the Servlet and is getting passed to the javascript through Ajax. Now in JavaScript the AjaxResponse Function reads the JSON object by calling eval function. eval function is inbuild function of javascript and here it is responsible for reading JSON Object data and will assign the data to javascript variable


Custom Search

Popular Articles:

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • email
  • IndianPad
  • LinkedIn
  • Live
  • MySpace
  • Netvibes
  • RSS
  • Technorati
  • Yahoo! Bookmarks
  • Yahoo! Buzz
  • Reddit
  • Add to favorites
  • PDF
  • Twitter
Categories: Java Tags: ,
  1. jameer
    April 15th, 2009 at 05:15 | #1

    i created one json file with some pair of values and i displayed those values in button click of html page.
    its working properly in fire fox(i.e json values get displayed in page using firefox)but not displaying values in page while using internet explorer.what is reason iam not getting

  1. No trackbacks yet.