文章17 | 阅读 10266 | 点赞0
本文内容大多基于官方文档和网上前辈经验总结,经过个人实践加以整理积累,仅供参考。
思考如何解析如下两个 JSON 字符串
{
"id":1,
"user":
{ "account":"admin", "password":"123456", "name":"John", "age":30 },
"memo":"xxx" }
{
"id":2,
"user":
[{ "account":"admin", "password":"123456", "name":"John", "age":30 },{ "account":"user1", "password":"654321", "name":"Leo", "age":22 }],
"memo":"yyy" }
这两个字符串除了“user”属性内容不同外,其他属性都是相同的,通常需要新建两个 POJO 类分别对应这两个 JSON 字符串进行解析
第一个 JSON 字符串对应的 POJO 类
class JsonObject {
private int id;
private User user;
private String memo;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getMemo() {
return memo;
}
public void setMemo(String memo) {
this.memo = memo;
}
}
第二个 JSON 字符串对应的 POJO 类
class JsonObject {
private int id;
private List<User> user;
private String memo;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public List<User> getUser() {
return user;
}
public void setUser(List<User> user) {
this.user = user;
}
public String getMemo() {
return memo;
}
public void setMemo(String memo) {
this.memo = memo;
}
}
如果使用泛型只需要新建如下一个 POJO 类
class JsonObject<T> {
private int id;
private T user;
private String memo;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public T getUser() {
return user;
}
public void setUser(T user) {
this.user = user;
}
public String getMemo() {
return memo;
}
public void setMemo(String memo) {
this.memo = memo;
}
@Override
public String toString() {
return "JsonObject [id=" + id + ", user=" + user + ", memo=" + memo + "]";
}
}
测试代码:
@Test
public void test() {
Gson gson = new Gson();
String jsonStr1 = "{\"id\":1,"
+ "\"user\":{\"account\":\"admin\",\"password\":\"123456\",\"name\":\"John\",\"age\":30},"
+ "\"memo\":\"xxx\"}";
String jsonStr2 = "{\"id\":2,"
+ "\"user\":[{\"account\":\"admin\",\"password\":\"123456\",\"name\":\"John\",\"age\":30},"
+ "{\"account\":\"user1\",\"password\":\"654321\",\"name\":\"Leo\",\"age\":22}],"
+ "\"memo\":\"yyy\"}";
JsonObject<User> object1 = gson.fromJson(jsonStr1,
new TypeToken<JsonObject<User>>() {}.getType());
System.out.println(object1);
JsonObject<List<User>> object2 = gson.fromJson(jsonStr2,
new TypeToken<JsonObject<List<User>>>() {}.getType());
System.out.println(object2);
}
运行测试结果:
当然也可以通过以下方式解析
@Test
public void test() {
Gson gson = new Gson();
String jsonStr1 = "{\"id\":1,"
+ "\"user\":{\"account\":\"admin\",\"password\":\"123456\",\"name\":\"John\",\"age\":30},"
+ "\"memo\":\"xxx\"}";
String jsonStr2 = "{\"id\":2,"
+ "\"user\":[{\"account\":\"admin\",\"password\":\"123456\",\"name\":\"John\",\"age\":30},"
+ "{\"account\":\"user1\",\"password\":\"654321\",\"name\":\"Leo\",\"age\":22}],"
+ "\"memo\":\"yyy\"}";
JsonObject<User> object1 = gson.fromJson(jsonStr1, JsonObject.class);
System.out.println(object1);
JsonObject<List<User>> object2 = gson.fromJson(jsonStr2, JsonObject.class);
System.out.println(object2);
}
运行测试结果:
不过这样解析会有类型未检查的警告
Type safety: The expression of type JsonObject needs unchecked conversion to conform to JsonObject<User>
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/Silent_Paladin/article/details/54096509
内容来源于网络,如有侵权,请联系作者删除!