程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> JAVA應用Gson解析json數據實例解析

JAVA應用Gson解析json數據實例解析

編輯:關於JAVA

JAVA應用Gson解析json數據實例解析。本站提示廣大學習愛好者:(JAVA應用Gson解析json數據實例解析)文章只能為提供參考,不一定能成為您想要的結果。以下是JAVA應用Gson解析json數據實例解析正文


JSON(JavaScript Object Notation) 是一種輕量級的數據交流格局,易於浏覽和編寫,同時也易於機械解析和生成。同XML一樣是一種“傳輸格局”。JSON采取與編程說話有關的文本格局,便於數據傳輸、存儲、交流。

封裝類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;
}
}

另外一個封裝類GsonDataAnalysis:

public class GsonDataAnalysis {
private List<Attribute> list;
public List<Attribute> getList() {
return list;
}
public void setList(List<Attribute> list) {
this.list = list;
}
}

測試類Main辦法:

/**
* Gson解析json數據
* @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號:" + gda.getList().get(i).getId() + " ");
System.out.print("姓 名:" + gda.getList().get(i).getName() + " ");
System.out.println("年 齡:" + gda.getList().get(i).getAge());
}
}
}

注:在應用Gson類的時刻在工程中要添加Gson.jar包。

上面給年夜家引見java 解析json格局數據

有時刻能夠會用到json格局停止數據的傳輸,那末怎樣把吸收到的數據解析出來呢? 上面引見兩種解析json數據的辦法:

1、經由過程谷歌的Gson來停止解析:

json數據:sTotalString = {"message":"success","result":[{"surveyid":"1","surveyname":"B"}{surveyid":"2","surveyname":"C"}]};

兩個VO類:

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;
} 
}

json格局轉換成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、經由過程json-org.jar包停止解析:

json數據:sTotalString = {"message":"success","result":[{"surveyid":"1","surveyname":"B"}{surveyid":"2","surveyname":"C"}]};

一個VO類:

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;
}
}

json格局轉換成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);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved