服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - JAVA教程 - JAVA使用Gson解析json数据实例解析

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

2020-04-08 14:35jerrylsxu JAVA教程

JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,易于阅读和编写,同时也易于机器解析和生成。接下来通过本文给大家介绍JAVA使用Gson解析json数据实例解析,需要的朋友参考下吧

JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,易于阅读和编写,同时也易于机器解析和生成。同XML一样是一种“传输格式”。JSON采用与编程语言无关的文本格式,便于数据传输、存储、交换。

封装类Attribute:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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:

?
1
2
3
4
5
6
7
8
9
public class GsonDataAnalysis {
private List<Attribute> list;
public List<Attribute> getList() {
return list;
}
public void setList(List<Attribute> list) {
this.list = list;
}
}

测试类Main方法:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* 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类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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类型:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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类型:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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();
}
}

 

延伸 · 阅读

精彩推荐