Home > Java > JSON in JAVA

JSON in JAVA

JSON è ampiamente utilizzato in Web Technology per il trasferimento dei dati in JavaScript. Ma con AJAX che entrano in foto JSON è diventato il più popolare strumento 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 usare il pacchetto JSON in Java.
Il pacchetto JSON che userò è a partire da: JAVA JSON Package
Questo articolo vuole dare un breve sguardo su di come si possa inviare i dati di grandi dimensioni da JSP / Servlet al cliente Page con JSON e AJAX.

Il pacchetto JSON utilizzato qui è possibile inviare i dati in due formati diversi e JSONArray 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

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

Pagina HTML lettura del valore di JSON:

 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 nuovo ActiveXObject = ("Msxml2.XMLHTTP");) catch (e) (try (XMLHTTP = new ActiveXObject ("Microsoft.XMLHTTP");) catch (e) (try (XMLHTTP = new XMLHttpRequest ();) catch (e) (XMLHTTP = false;))) XMLHTTP return;) XMLHTTP var = getHTTPObject nuovo (); / / Inizializzare nuovo oggetto funzione Connect2RemoteSite Ajax (url, funcname, parametro) (try (if (XMLHTTP) (XMLHTTP. open ("POST", url, true); / / URL sarà la XMLHTTP pagina servlet. onreadystatechange = funcname; XMLHTTP. setRequestHeader ('Content-Type' applicazione, '/ x-www-form-urlencoded'); XMLHTTP. setRequestHeader ("Content-Length", parametro. lunghezza); XMLHTTP. send (parametro);) else (documento. getElementById ( "rotateimage"). stile. display = "none"; alert ("Il vostro sostegno doesnt Browser Ajax");)) catch (e) (alert ("Alcuni sconosciuto errore. Riprovare" + e);) AjaxResponse) function () (if (xmlhttp. readyState == 4) (if (xmlhttp. status == 200) (/ / servletdetail detiene il Details servletdetail JSONArray = eval (xmlhttp. responseText);))) </ script> < / head> <body> </ body> </ html> 

Il tuo indirizzo email:


Qui ho dato l'attuazione JSON con AJAX. JSON oggetto è sempre creato nel Servlet e sta ottenendo passato al javascript tramite Ajax. Ora, in JavaScript Funzione AjaxResponse legge l'oggetto JSON chiamando la funzione eval. Eval funzione è inbuild funzione di javascript e qui è responsabile della lettura dei dati JSON Object e assegnerà i dati a variabile javascript

Custom Search

Articoli popolari:

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
Tags: , Categorie: Java Tag: ,
  1. Jameer
    15 aprile 2009 alle 05:15 | # 1

    Ho creato un file JSON con qualche coppia di valori e quelli visualizzati i valori in fare clic sul pulsante della pagina html.
    il buon funzionamento in fire fox (valori json cioè avere visualizzato nella pagina usando Firefox) ma non nella pagina di visualizzazione dei valori durante l'utilizzo di internet è explorer.what iam la ragione non sempre

  1. Nessun trackback ancora.