【问题标题】:How to serialize/deserialize (JSON) ArrayLIst<A> which has an ArrayLIst<B> as field in Java如何序列化/反序列化(JSON)ArrayLIst<A>,它有一个 ArrayLIst<B> 作为 Java 中的字段
【发布时间】: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


    【解决方案1】:

    如果是收集,则必须指定类型。

    See a Guide User

    在你的情况下,我认为这会解决:

    Type collectionType = new TypeToken<Collection<A>>(){}.getType();
    Collection<A> list = gson.fromJson(json, collectionType);   
    

    A 是你的班级。

    【讨论】:

    • 感谢您的回答。 Collection 像 ArrayLIst 吗?因为我需要使用 ArrayList(在 ListView 中显示)。即使我的 A 类没有在 JSONObject 中转换其 B 的 ArrayList 的方法,我可以将它与 ArrayList 一起使用吗?
    • 是的。 Collection 是为 ArrayList、HashMap 等实现的 java 接口,因此您可以在这种情况下执行此操作,例如:ArrayList&lt;A&gt; myArray = (ArrayList)list; 没有问题。我认为您将不再需要定义类型。
    • 好的,我明白了,但我真的不明白 Java 如何知道如何使用 Collection 将我的 ArrayList 中的所有 B 对象“转换”为 JSONObject 并将它们存储在另一个 JSONObject 中。
    • 你想知道它是怎么做的吗?如果是这样,GSON 使用 Reflection 在运行时访问类的方法和属性。
    • 啊,好吧,除了 Gson 对象之外我不需要实现任何其他东西来序列化和反序列化?因为我试图序列化我的对象并且它失败了。你有关于如何在代码中实现它的具体代码吗,因为这对我来说是全新的,我对 Collection 等不太了解......
    【解决方案2】:

    您是否尝试使用 Parcelable?试试这样的东西(伪代码):

    public class ParcelableObject implements Parcelable{
    
    List<ObjectB> _customObjectList = new ArrayList<ObjectB>();
    
    @Override
    public void writeToParcel(Parcel outParcel_, int arg1) {
    outParcel_.writeTypedList(_customObjectList); //where _customObjectList is your list variable
    }
    
    
    //Then in your constructor which takes in a Parcel:
    
    public ParcelableObject(Parcel parcelIn_) {
    parcelIn_.readTypedList(_customObjectList, ObjectB.CREATOR);
    }
    
    }
    

    对象B

    public class ObjectB implements Parcelable{
    
    int _randomInt;
    
        public static final Parcelable.Creator<ObjectB> CREATOR = new Parcelable.Creator<ObjectB>() {
            @Override
            public ObjectBcreateFromParcel(Parcel in) {
                return new ObjectB(in);
            }
    
            @Override
            public ObjectB[] newArray(int size) {
                return new ObjectB[size];
            }
        };
    
        public ObjectB(Parcel parcelIn_) {
              _randomInt = parcelIn_.readInt();
    
        }
    
    
    
        @Override
        public void writeToParcel(Parcel out_, int flags) {
            out_.writeInt(_randomInt);
        }
    }
    

    【讨论】:

    • 感谢您的回答。我没有使用 Parcelable,因为我需要 JSONObject,才能拥有 .json 文件。
    • @gallium 为什么不试试这个呢,写一个 POJO 将 JSON 数据封装成有意义的东西。例如,如果您的 JSON 对象包含有关人员的数据,那么您将创建一个 POJO,其中包含用于您的 Person 对象的字段和 getter/setter。然后,您可以实现 Parcelable 并执行我所描述的操作。这样,您的对象就有一个标准接口,并且可以比原始 JSON 数据更容易地使用。
    • 我不知道你说的是不是这样,但我有一个名为 ClientCenter 的类,它是静态的,其中包含我的数据:客户端类 (A) 的 ArrayList。客户端类有一个字段,它是类债务 (B) 的 ArrayList。我想知道如何使用简单的序列化程序,因为我正在使用this book 学习 Android 编程,他们不使用 Parcelable 或 Gson,但他们没有解释如何使用自定义对象作为字段序列化对象......跨度>
    【解决方案3】:

    最好是做这样的事情:

    List<YourObject> myList = new Genson().deserialize(dta.toString(), List.class);
    

    【讨论】:

      猜你喜欢
      • 2019-03-17
      • 1970-01-01
      • 1970-01-01
      • 2013-05-07
      • 1970-01-01
      • 2019-05-02
      • 2014-04-12
      • 2015-04-14
      • 2012-02-05
      相关资源
      最近更新 更多