解説
GSONの使い方を簡単に纏めておく。更新日:2017年3月25日
準備
下記では、GSONバージョン2.8.0を使用している。
詳細は、 A Java library to convert JSON to Java objects and vice-versa - Google Project Hostingを参照。
詳細は、 A Java library to convert JSON to Java objects and vice-versa - Google Project Hostingを参照。
GSON使用準備
dependencies
{
compile 'com.google.code.gson:gson:2.8.0'
}
サンプル・クラス
重要クラスを使用しなくても、利用は可能だが、クラスを作成した方が様々なデータを一括管理できるために、作成する方が良い。
Post.java
public class Post
{
private String mTitle;
private String mBody;
private String mName;
private String mUid;
private String mQuestionUid;
private int mGenre;
private byte[] mBitmapArray;
private ArrayList<Comment> mCommentList;
public String getTitle()
{
return mTitle;
}
public String getBody()
{
return mBody;
}
public String getName()
{
return mName;
}
public String getUid()
{
return mUid;
}
public String getQuestionUid()
{
return mQuestionUid;
}
public int getGenre()
{
return mGenre;
}
public byte[] getImageBytes()
{
return mBitmapArray;
}
public ArrayList<Answer> getAnswers()
{
return mCommentList;
}
public Post( String title, String body, String name, String uid, String questionUid, int genre, byte[] bytes, ArrayList<Comment> comments )
{
mTitle = title;
mBody = body;
mName = name;
mUid = uid;
mQuestionUid = questionUid;
mGenre = genre;
mBitmapArray = bytes.clone();
mCommentList = comments;
}
}
Comment.java
public class Comment
{
private String mBody;
private String mName;
private String mUid;
private String mCommentID;
public Comment( String body, String name, String uid, String commentID )
{
mBody = body;
mName = name;
mUid = uid;
mCommentID = commentID;
}
public String getBody()
{
return mBody;
}
public String getName()
{
return mName;
}
public String getUid()
{
return mUid;
}
public String getCommentID()
{
return mCommentID;
}
}
シリアライズ - toJson()
API
- String toJson(JsonElement jsonElement)
- Converts a tree of JsonElements into its equivalent JSON representation.
- void toJson(JsonElement jsonElement, Appendable writer)
- Writes out the equivalent JSON for a tree of JsonElements.
- void toJson(JsonElement jsonElement, JsonWriter writer)
- Writes the JSON for jsonElement to writer.
- String toJson(Object src)
- This method serializes the specified object into its equivalent Json representation.
- void toJson(Object src, Appendable writer)
- This method serializes the specified object into its equivalent Json representation.
- String toJson(Object src, Type typeOfSrc)
- This method serializes the specified object, including those of generic types, into its equivalent Json representation.
- void toJson(Object src, Type typeOfSrc, Appendable writer)
- This method serializes the specified object, including those of generic types, into its equivalent Json representation.
- void toJson(Object src, Type typeOfSrc, JsonWriter writer)
- Writes the JSON representation of src of type typeOfSrc to writer.
シリアライズ - toJson()
Post post = new Post( ... );
Gson gson = new Gson();
String json = gson.toJson( post );
デシリアライズ - fromJson()
API
-
T fromJson(JsonElement json, Class classOfT) - This method deserializes the Json read from the specified parse tree into an object of the specified type.
-
T fromJson(JsonElement json, Type typeOfT) - This method deserializes the Json read from the specified parse tree into an object of the specified type.
-
T fromJson(JsonReader reader, Type typeOfT) - Reads the next JSON value from reader and convert it to an object of type typeOfT.
-
T fromJson(Reader json, Class classOfT) - This method deserializes the Json read from the specified reader into an object of the specified class.
-
T fromJson(Reader json, Type typeOfT) - This method deserializes the Json read from the specified reader into an object of the specified type.
-
T fromJson(String json, Class classOfT) - This method deserializes the specified Json into an object of the specified class.
-
T fromJson(String json, Type typeOfT) - This method deserializes the specified Json into an object of the specified type.
デシリアライズ - fromJson()
Gson gson = new Gson();
String json = ...;
Post post = gson.fromJson( json, Post.class );