【发布时间】:2013-12-14 20:42:40
【问题描述】:
我是 Android 编程领域的新手,我在使用 JSON 序列化和反序列化自定义对象的 ArrayList 时遇到了一些问题。我知道如何序列化一个对象,但如果它有一个 ArrayList 作为字段,我就不知道了。
我找到了 GSON 库,但我真的不知道如何使用它。
我有一个 class A 的 ArrayList,它有一个 class B 的 ArrayList。我正在使用 toJSON() 方法对 A 类进行序列化:
public class A
{
//Some fields like title, id...
private String mTitle;
private ArrayList<B> mArray; //I don't know how to serialize/deserialize this field
public A(){...}
public A(JSONObject json){...}
public JSONObject toJSON() throws JSONException //method I use to convert my object into a JSONObject
{
JSONObject json = new JSONObject();
json.put("TITLE",mTitle);
return json;
}
}
public class B
{
//some fields like a double, String...
public B(){...}
}
【问题讨论】:
标签: java android json serialization arraylist