Java uses Gson to parse JSON data instance resolution _java

Source: Internet
Author: User
Tags getmessage

JSON (JavaScript Object notation) is a lightweight data interchange format that is easy to read and write, and is also easy to machine parse and generate. The same as XML is a "transport format." JSON is a programming language-independent text format that facilitates data transfer, storage, and exchange.

Packaging Category attribute:

public class Attribute {
private int id;
private String name;
private int age;
public int getId () {return
ID;
}
public void setId (int id) {
this.id = ID;
}
Public String GetName () {return
name;
}
public void SetName (String name) {
this.name = name;
}
public int getage () {return age
;
}
public void Setage (int age) {
this.age = age;
}
}

Another package class Gsondataanalysis:

public class Gsondataanalysis {
private list<attribute> List;
Public list<attribute> GetList () {return
List;
}
public void setlist (list<attribute> list) {
this.list = list;
}
}

Test class Main Method:

/**
* Gson Parse JSON data
* @author foreverlover
* @version 2015-04-19/public
class Gsontest {
public static void Main (string[] args) {
String stotalstring = "{\ list\": ["+
" {\ id\]: \ "1\", \ "name\": \ "Tom\", \ "age\": \ "12\"}, "+
" {\ "id\": \ "2\", \ "name\": \ "marry\", \ "age\": \ "18\"} "+
"]} ";
Gson Gson = new Gson ();
Gsondataanalysis Gda = Gson.fromjson (
stotalstring, gsondataanalysis.class);
for (int i = 0; i < gda.getlist (). Size (); i + +) {
System.out.print ("ID Number:" + gda.getlist (). get (i). GetId () + ""); 
   system.out.print ("Name:" + gda.getlist (). get (i). GetName () + "");
System.out.println ("Age:" + gda.getlist (). get (i). Getage ());

Note: When using the Gson class, add the Gson.jar package to the project.

Here to introduce Java parsing JSON format data

Sometimes the data can be transmitted in JSON format, so how do you parse the data you receive? Here are two ways to parse JSON data:

1, through Google's Gson to analyze:

JSON data: stotalstring = {"message": "Success", "result": [{"Surveyid": "1", "Surveyname": "B"}{surveyid ":" 2 "," Surveyname ":" C "}]};

Two VO classes:

public class Surveyvo {
private String Surveyid;
Private String surveyname;
private String message;
Public String GetMessage () {return message
;
}
public void Setmessage (String message) {
this.message = message;
}
Public String Getsurveyid () {return
Surveyid;
}
public void Setsurveyid (String surveyid) {
This.surveyid = Surveyid;
}
Public String Getsurveyname () {return
surveyname;
}
public void Setsurveyname (String surveyname) {
this.surveyname = surveyname;
}
}
public class Surveylistvo {
private String message;
Private list<surveyvo> result;
Public String GetMessage () {return message
;
}
public void Setmessage (String message) {
this.message = message;
}
Public list<surveyvo> GetResult () {return result
;
}
public void Setresult (list<surveyvo> result) {
This.result = result;
} 
}

The JSON format is converted to type object:

public class Fromgson () {
stotalstring = {' message ': ' Success ', ' result ': [{' Surveyid ': ' 1 ', ' surveyname ': ' B '} ' Surveyid ":" 2 "," Surveyname ":" C "}]};
Gson Gson = new Gson ();
Surveylistvo Surveylistvo = Gson.fromjson (stotalstring,
surveylistvo.class);
for (int i = 0; i < Surveylistvo.getresult (). Size (); i++) {
System.out.print (Surveylistvo.getresult (). get (i)
. Getsurveyid ());
PRINT:1////2
System.out.print (Surveylistvo.getresult (). Get (i)
. Getsurveyname ());
print:b///c
System.out.print (Surveylistvo.getmessage ());
}

2, through the Json-org.jar package to resolve:

JSON data: stotalstring = {"message": "Success", "result": [{"Surveyid": "1", "Surveyname": "B"}{surveyid ":" 2 "," Surveyname ":" C "}]};

A VO class:

public class Surveyvo {
private String Surveyid;
Private String surveyname;
Public String Getsurveyid () {return
Surveyid;
}
public void Setsurveyid (String surveyid) {
This.surveyid = Surveyid;
}
Public String Getsurveyname () {return
surveyname;
}
public void Setsurveyname (String surveyname) {
this.surveyname = surveyname;
}
}

The JSON format is converted to type object:

 public class Fromjson () {stotalstring = {' message ': ' Success ', ' result ': [{' Surveyid ': ' 1
"," Surveyname ":" B "} {" Surveyid ":" 2 "," Surveyname ":" C "}]};
Jsonobject JSON;
try {json = new jsonobject (stotalstring);
Jsonarray results = Json.getjsonarray ("result");
for (int i = 0; i < results.length (); i++) {Surveyvo Surveyvo = new Surveyvo ();
Jsonobject result = Results.getjsonobject (i);
System.out.println (result.getstring ("Surveyid") + "" +result.getstring ("Surveyname"));
Surveyvo.setsurveyid (result.getstring ("Surveyid"));
Surveyvo.setsurveyname (result.getstring ("Surveyname"));
Surveyvolist.add (SURVEYVO); 
The catch (Jsonexception e) {e.printstacktrace ()}} 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.