JSON in JAVA

JSON è ampiamente utilizzato in Web Technology per il trasferimento dei dati in JavaScript. Ma con AJAX entrata in JSON foto è diventato lo strumento più importante per l'invio di dati da pagina web remota alla pagina chiamante. Beh ci sono diversi pacchetti disponibili per l'attuazione JSON in Java. Qui sto usando uno dei pacchetti per mostrare come si possa utilizzare il pacchetto di JSON in Java.
Il pacchetto JSON che verrà utilizzato è a partire da: JAVA JSON Package
Questo articolo vuole dare un piccolo assaggio di come si possa inviare i dati di grandi dimensioni da JSP / Servlet al cliente Page utilizzando AJAX e JSON.

Il pacchetto JSON usato qui è possibile inviare i dati in due diversi formati JSONArray e JSONObject. Tha differenza principale tra JSONArray e JSONObject è che JSONArray usa ArrayList per memorizzare i dati, mentre JSONObject HashMap utilizza per memorizzare i dati.

Java Servlet attuazione JSON

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

HTML valore JSON lettura Pagina:

 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 > <Html> <head> <script language = "Javascript"> getHTTPObject function () (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 = getHTTPObject new (); / / inizializzare Nuova funzione Ajax Object Connect2RemoteSite (url, funcname, parametro) (try (if (xmlhttp) (XMLHTTP. open ( "POST", url, true); / / URL sarà la XMLHTTP servlet pagina. onreadystatechange = funcname; XMLHTTP. setRequestHeader ( 'Content-Type applicazione', '/ x-www-form-urlencoded'); setRequestHeader. xmlhttp ( "Content-Length", parametro. length); XMLHTTP. invia (parametro);) else (documento. getElementById ( "rotateimage"). stile. display = "none"; alert ( "Your browser doesnt support Ajax");)) catch (e) (alert ( "Alcuni Errore sconosciuto si era verificato. Riprovare" + e);) AjaxResponse) function () (if (xmlhttp. readyState == 4) (if (xmlhttp. status == 200) (/ / servletdetail detiene il servletdetail JSONArray Details = eval (xmlhttp. responseText);))) </ script> < / head> <body> </ body> </ html> 

La tua email:


Qui ho dato l'attuazione JSON con AJAX. Oggetto JSON sta creato nel Servlet e si sta passata al javascript tramite Ajax. Ora in JavaScript Funzione AjaxResponse legge l'oggetto JSON chiamando la funzione eval. Funzione eval è funzione inbuild di javascript e qui è responsabile per la lettura dei dati JSON Object e assegnerà i dati variabili javascript

Custom Search


Articoli popolari:

Tags: , Categorie: Tag Java:
  1. Jameer
    15 aprile 2009 at 05:15 | # 1

    Ho creato un file json con una coppia di valori e di quelli visualizzati i valori in fare clic sul pulsante della pagina html.
    il buon funzionamento in Fire Fox (valori json ossia vengono visualizzate nella pagina con Firefox), ma non la visualizzazione dei valori nella pagina durante l'utilizzo di internet è explorer.what iam ragione non ricevendo

  1. Ancora nessun trackback.