JSON (JavaScript Object Notation) es un formato de intercambio de datos liviano que es fácil de leer y escribir, y también es fácil de analizar y generar. Al igual que XML, es un "formato de transferencia". JSON adopta un formato de texto independiente de los lenguajes de programación, que es conveniente para la transmisión de datos, el almacenamiento y el intercambio.
Atributo de clase de encapsulación:
Atributo de clase pública {private int id; private string name; private int a age; public int getId () {return id;} public void setid (int id) {this.id = id = id;} public String String getName () {return name;} public void setname (name) {this.name = name = name;} public int edad;}} Otra clase de encapsulación GSondataanálisis:
public class gSondataanalysis {Lista privada <Attribute> List; public List <Attribute> getList () {return list;} public void setList (list <tribute> list) {this.list = list;}}Prueba el método principal:
/*** Gson analiza los datos JSON* @author ForeverLover* @version 2015-04-19*/public class gsontest {public static void main (string [] args) {String stotalString = "{/" list/": [" +"{/" id/":/" 1/",/" name/"tom/",/"edad/":/"12/". +"{/" id/":/" 2/",/" name/":/" Marry/",/" Age/":/" 18/"}" +"]}"; gson gson = new gson (); gsondataanalysis gda = gson.fromjson (stotalstring, gsondataanalysysysysysysysysysysysysysysysysysys.class); para (int i = 0; i <i GDA.GetList (). Size (); gda.getList (). get (i) .getage ());}}}Nota: Cuando use la clase GSON, agregue el paquete GSON.JAR al proyecto.
A continuación es introducir Java para analizar los datos del formato JSON
A veces, el formato JSON se puede usar para transmitir datos, entonces, ¿cómo puedo analizar los datos recibidos? Los siguientes son dos métodos para analizar los datos JSON:
1. Análisis a través del GSON de Google:
JSON Data: stotalString = {"mensaje": "éxito", "resultado": [{"sospechoso": "1", "Surveyname": "b"} {startingID ":" 2 "," Surveyname ":" C "}]};
Dos clases de VO:
public class SurveyVo {private String SurveId; private String Surveyname; privado String Message; public String getMessage () {return Message;} public void setMessage (string Message) {this.message = Message;} public String getSurvey () {return SurveId;} public void setSurveyID (string sporting) {this.surveyDID = SurveyID;} string string (SurveyDid;} string (string string (SurveyidMeReName (String String () () {) Surveyname;} public void setSurveyname (String Surveyname) {this.surveyname = Surveyname;}} public class SurveylistVo {private String Message; Private List <SurningVo> Result; public String getMessage () {return Message;} public void setMessage (string Message) {this.message;} list <LIT <ing Surve {Result de retorno;} public void setResult (list <suringvo> resultado) {this.result = resultado;}}Convertir el formato JSON al tipo de objeto:
public class fromGson () {stotalString = {"mensaje": "éxito", "resultado": [{"sospechoso": "1", "Surveyname": "B"} {"SurveyID": "2", "Surveyname": "C"}]; GSON GSON = new Gson (); SurvanyistVo SurvylistVo = = = = = = = = = = = = = = = = = = = = = = = = = = = gson.fromjson (stotalString, Surveylistvo.class); para (int i = 0; i <SurveylistVo.getResult (). size (); i ++) {System.out.print (SurveylistVo.getResult (). Get (i) .getSurveyID (); imprime::: 1 //// 2System.out.print (SurveylistVo.getResult ().2. Análisis a través del paquete JSON-ORG.JAR:
JSON Data: stotalString = {"mensaje": "éxito", "resultado": [{"sospechoso": "1", "Surveyname": "b"} {startingID ":" 2 "," Surveyname ":" C "}]};
Una clase de Vo:
public class SurveyVo {private String SurveDId; private String Surveyname; public String getSurveyId () {return SurveDId;} public void setSurveyId (String SurveyId) {this.surveyId = sospeche;} public String getSurveyname () {return Surveyname;} public void setsurveyname (string Surveyname) {this.surveNameConvertir el formato JSON al tipo de objeto:
public class fromjson () {stotalString = {"mensaje": "éxito", "resultado": [{"sospecheD": "1", "Surveyname": "b"} {"sospechoso": "2", "Surveyname": "c"}]; jsonObject JSON; intit {json = neo jsonObject (stotalString; json.getjsonArray ("resultado"); para (int i = 0; i <results.length (); i ++) {SurveyVo SurveyVo = new SurveyVo (); jsonObject result = results.getJsonObject (i); System.out.println (resultado.getString ("SurveId" ")+" "+resultado.getString (" Surveyname ")); SurveyVo.SetSurveyId (result.getString (" SurveyID ")); SurveyVo.SetSurveyname (result.getString (" Surveyname "); SurveyVolist.Add (SurveyVO);}} Catch (JSoonexception e) {E.PRINTSTRACE ();}}}}