Gsonは簡単なjson変換を実現する
4873 ワード
ここで簡単な例を示します
エンティティ
結果クラスを返す
単純なシーケンス化parser
テスト
結果:
注意:
逆jsonシーケンス化の場合、汎用型の実際のタイプを取得するには、次のように取得できます.そうしないと、gsonは結果をobjectのStringMap形式で返します.
エンティティ
public class User {
private Integer id;
private String username;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", date=" + date
+ "]";
}
}
結果クラスを返す
public class ResultMessage<T> {
private String token;
private Integer code;
private T data;
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
@Override
public String toString() {
return "ResultMessage [token=" + token + ", code=" + code + ", data="
+ data + "]";
}
}
単純なシーケンス化parser
public class JsonParser {
private static final Gson gson;
static {
gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:SS").create();
}
public static <T> T deserializeFromJson(String json, Class<T> clazz) {
return gson.fromJson(json, clazz);
}
public static <T> T deserializeFromJson(String json, Type type) {
return gson.fromJson(json, type);
}
public static String serializeToJson(Object obj) {
return gson.toJson(obj);
}
}
テスト
// null
ResultMessage<User> result1 = new ResultMessage<User>();
result1.setToken(getToken());
result1.setCode(ResultCode.SUCCESS);
result1.setData(null);
System.out.println(JsonParser.serializeToJson(result1));
// object
ResultMessage<User> result2 = new ResultMessage<User>();
User user = new User();
user.setId(123);
user.setUsername("name");
user.setDate(new Date());
result2.setToken(getToken());
result2.setCode(ResultCode.SUCCESS);
result2.setData(user);
System.out.println(JsonParser.serializeToJson(result2));
// list
ResultMessage<List<User>> result3 = new ResultMessage<List<User>>();
List<User> users = new ArrayList<User>();
for (int i = 0; i < 10; i++) {
User u = new User();
u.setId(i);
u.setUsername("name" + i);
u.setDate(new Date());
users.add(u);
}
result3.setToken(getToken());
result3.setCode(ResultCode.SUCCESS);
result3.setData(users);
System.out.println(JsonParser.serializeToJson(result3));
// fail
ResultMessage<String> result4 = new ResultMessage<String>();
result4.setToken(getToken());
result4.setCode(ResultCode.REEOR);
result4.setData("you haven't login");
System.out.println(JsonParser.serializeToJson(result4));
結果:
{"token":"818387524bfff5bf4eb2d2e49aab73eb","code":1}
{"token":"818387524bfff5bf4eb2d2e49aab73eb","code":1,"data":{"id":123,"username":"name","date":"2014-10-28 12:46:922"}}
{"token":"818387524bfff5bf4eb2d2e49aab73eb","code":1,"data":[{"id":0,"username":"name0","date":"2014-10-28 12:46:926"},{"id":1,"username":"name1","date":"2014-10-28 12:46:926"},{"id":2,"username":"name2","date":"2014-10-28 12:46:926"},{"id":3,"username":"name3","date":"2014-10-28 12:46:926"},{"id":4,"username":"name4","date":"2014-10-28 12:46:926"},{"id":5,"username":"name5","date":"2014-10-28 12:46:926"},{"id":6,"username":"name6","date":"2014-10-28 12:46:926"},{"id":7,"username":"name7","date":"2014-10-28 12:46:926"},{"id":8,"username":"name8","date":"2014-10-28 12:46:926"},{"id":9,"username":"name9","date":"2014-10-28 12:46:926"}]}
{"token":"818387524bfff5bf4eb2d2e49aab73eb","code":-1,"data":"you haven\u0027t login"}
注意:
逆jsonシーケンス化の場合、汎用型の実際のタイプを取得するには、次のように取得できます.そうしないと、gsonは結果をobjectのStringMap形式で返します.
Type type = new TypeToken<ResultMessage<List<User>>>() {}.getType()