JSON (JavaScript Object Notation) is a lightweight data exchange format that is easy to read and write, and is also easy to machine parse and generate. Like XML, it is a "transfer format". JSON adopts a text format independent of programming languages, which is convenient for data transmission, storage and exchange.
Encapsulation class 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 encapsulation 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 the Main method:
/*** Gson parses 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.
Below is to introduce Java to parse json format data
Sometimes, json format may be used to transmit data, so how can I parse the received data? The following are two methods of parsing json data:
1. Analysis through Google's Gson:
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;} }Convert json format to object type:
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////2System.out.print(surveyListVO.getResult().get(i).getSurveyName());print: B////CSystem.out.print(surveyListVO.getMessage());}}2. Analysis through json-org.jar package:
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;}}Convert json format to object type:
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);}} catch (JSONException e) {e.printStackTrace();}}