java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > java gson解析json

JAVA使用Gson解析json数据实例解析

作者:jerrylsxu

JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,易于阅读和编写,同时也易于机器解析和生成。接下来通过本文给大家介绍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();
}
}
您可能感兴趣的文章:
阅读全文