简体   繁体   English

如何序列化对象的ArrayList?

[英]How to serialize ArrayList of objects?

I want to serialize an arraylist of Item but it doesn't work.... 我想序列化Item的数组列表,但是不起作用...。

my Item class extends Stuff class and has some subclasses . 我的Item类扩展了Stuff类,并具有一些子类

all of my classes implement Serilalizable. 我所有的类都实现Serilalizable。

i have this part : 我有这部分:

 try{ // Serialize data object to a file ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("MyData.ser")); out.writeObject(myData); out.close(); // Serialize data object to a byte array ByteArrayOutputStream bos = new ByteArrayOutputStream() ; out = new ObjectOutputStream(bos) ; out.writeObject(myData); out.close(); // Get the bytes of the serialized object byte[] buf = bos.toByteArray(); } catch (IOException e) { } 

my classes : 我的课程 :

 public class Stuff implements Serializeable{ .... some protected fields . . } public class Item extends Stuff implements Serializable{ ... .. . } and some subclasses of Item: public class FirstItem extends Item implements Serializable{ ... } public class SecondItem extends Item implements Serializable{ ... } ... I want to serialize an object contains ArrayList of <Item> that has objects of Item's subclasses (FirstItem,SecondItem,...) 

i think informations are sufficient... 我认为信息足够...

it's just a little mistake and now works correctly... I'm sorry for my stupid question. 这只是一个小错误,现在可以正常使用...对于我的愚蠢问题,我们深表歉意。

Thank you for your answers. 谢谢您的回答。

You can serialize class of ArrayList like this 您可以像这样序列化ArrayList的类

public class MyData implements Serializable {

    private long id;
    private String title;
    private ArrayList<String> tags;
    ...

    public String getTitle() {
    }
}

And to create serializable 并创建可序列化

    ArrayList<MyData> myData = new ArrayList<MyData>();

try{
    // Serialize data object to a file
    ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("MyData.ser"));
    out.writeObject(myData);
    out.close();

    // Serialize data object to a byte array
    ByteArrayOutputStream bos = new ByteArrayOutputStream() ;
    out = new ObjectOutputStream(bos) ;
    out.writeObject(myData);
    out.close();

    // Get the bytes of the serialized object
    byte[] buf = bos.toByteArray();
} catch (IOException e) {
}

I'm taking a leap here that you are trying to serialize to something like json? 我在这里取得了飞跃,您正在尝试序列化为json之类的东西?

if so, you can use jackson ( http://jackson.codehaus.org/ ) 如果是这样,您可以使用jackson( http://jackson.codehaus.org/

final ObjectMapper mapper = new ObjectMapper();
try
{
    final String jsonString = mapper.writeValueAsString( _integrationSettings );
    // do something with the string...
}
catch( IOException e )
{
    // use a logger to output error
}

You can also deserialize with jackson as well... 您还可以与杰克逊进行反序列化...

A more detailed example here: http://blog.inflinx.com/2012/05/10/using-jackson-for-javajson-conversion/ 此处有一个更详细的示例: http : //blog.inflinx.com/2012/05/10/using-jackson-for-javajson-conversion/

Note: you can do similar for XML. 注意:您可以对XML做类似的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM