用于处理JSON数据的工具有很多,搜的时候看到了Gson,所以就选择了这个,就是这么草率。
但是昨天要解析下面的json字符串,说白了我只是想要把data字段里面的东西都转成java对象,结果在网上搜了半天,找到的博客不是解析这种{"name":"xxx", "age":"19"}
类型的,就是解析[{"name":"xxx", "age":"19"}]
这种类型的,我要这种有何用???
{
"data": [
{
"createTime": "2018-11-05 14:12:39",
"groupDesc": "组1",
"groupKey": "group-test",
"id": "c14da77a-600d-4773-a94e-2fc337e72414",
"status": 1,
"systemKey": "AA",
"updateTime": "2018-11-05 14:12:38"
}
],
"message": "success",
"status": "0"
}
不过,经过我坚持不懈的努力,最终还是解决了这个问题。Maven项目,直接在pom中添加依赖:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
以下代码都是可以直接运行的,此处使用了unit的Test,可以在pom里面添加unit依赖:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
</dependency>
下面是测试代码,详细说明都在注释里面:
@Test
public void gsonTest() {
String simpleStr = "{\"name\":\"Sunny\",\"age\":19}";
String arraySimpleStr = "[{\"name\":\"Sunny\",\"age\":19}]";
String response = "{\"data\":[{" +
" \"createTime\": \"2018-11-05 14:12:39\"," +
"\"groupDesc\": \"组1\"," +
"\"groupKey\": \"group-test\"," +
"\"id\": \"c14da77a-600d-4773-a94e-2fc337e72414\"," +
"\"status\": 1," +
"\"systemKey\": \"AA\"," +
"\"updateTime\": \"2018-11-05 14:12:38\"" +
"}]," +
"\"message\": \"success\"," +
"\"status\": \"0\"" + "}";
Gson gson = new Gson();
// 调用fromJson方法直接对json字符串进行转换,传入json字符串,指定要转换成哪种类型的对象
SimplePojo simplePojo = gson.fromJson(simpleStr, SimplePojo.class);
System.out.println("SimpleStr result : " + simplePojo.toString());
// 对于解析数据有两种方式,此处因为数据里只有一条数据,就不遍历输出了
// 1. 指定转换后的对象为数组的形式SimplePojo[].class
SimplePojo[] arraySimplePojos = gson.fromJson(arraySimpleStr, SimplePojo[].class);
System.out.println("arraySimpleStr result : " + arraySimplePojos[0].toString());
// 2. 通过解析器直接转换成数组
// 定义一个解析器
JsonParser parser = new JsonParser();
JsonArray arraySimplePojos2 = parser.parse(arraySimpleStr).getAsJsonArray();
System.out.println("arraySimpleStr result2 : " + arraySimplePojos2.get(0).toString());
// 解析复杂类型的json字符串, list用于存放解析data数组生成的对象
ArrayList<GroupResponse> list = new ArrayList<GroupResponse>();
// 将数据先解析成JsonObject类型的对象
JsonObject object = parser.parse(response).getAsJsonObject();
// 通过字段名称获取到指定字段的数据, 此处可以看出data字段里是一个数组, 所以返回的是JsonArray类型的
JsonArray array = object.get("data").getAsJsonArray();
for (JsonElement element : array) {
// 遍历取出数组里的每个element, 此处就跟把一个把简单json字符串转成对象一样了
GroupResponse groupResponse = gson.fromJson(element, GroupResponse.class);
list.add(groupResponse);
}
System.out.println("Response result : " + list.toString());
}
输出结果:
SimpleStr result : SimplePojo{name='Sunny', age=19}
arraySimpleStr result : SimplePojo{name='Sunny', age=19}
arraySimpleStr result2 : {"name":"Sunny","age":19}
Response result : [GroupResponse{createTime='2018-11-05 14:12:39', groupDesc='组1', groupKey='group-test', id='c14da77a-600d-4773-a94e-2fc337e72414', status=1, systemKey='AA', updateTime='2018-11-05 14:12:38'}]
关于Gson的使用,先写这么多吧,里面还有很多强大的功能,项目比较紧张,其他功能后面再补充吧。
Emmm… 似乎还需要贴出来pojo类?
/**
* @author Peerless galaxy.
* @date 2018/11/6 10:59
*/
public class SimplePojo {
private String name;
private int age;
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;
}
@Override
public String toString() {
return "SimplePojo{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
/**
* @author Peerless galaxy.
* @date 2018/11/6 11:07
*/
public class GroupResponse {
String createTime;
String groupDesc;
String groupKey;
String id;
int status;
String systemKey;
String updateTime;
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
public String getGroupDesc() {
return groupDesc;
}
public void setGroupDesc(String groupDesc) {
this.groupDesc = groupDesc;
}
public String getGroupKey() {
return groupKey;
}
public void setGroupKey(String groupKey) {
this.groupKey = groupKey;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getSystemKey() {
return systemKey;
}
public void setSystemKey(String systemKey) {
this.systemKey = systemKey;
}
public String getUpdateTime() {
return updateTime;
}
public void setUpdateTime(String updateTime) {
this.updateTime = updateTime;
}
@Override
public String toString() {
return "GroupResponse{" +
"createTime='" + createTime + '\'' +
", groupDesc='" + groupDesc + '\'' +
", groupKey='" + groupKey + '\'' +
", id='" + id + '\'' +
", status=" + status +
", systemKey='" + systemKey + '\'' +
", updateTime='" + updateTime + '\'' +
'}';
}
}