Java 如何序列化对象的ArrayList?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23793885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to serialize ArrayList of objects?
提问by armin.mohammadi
I want to serialize an arraylist of Itembut it doesn't work....
my Itemclass extends Stuffclass and has some subclasses.
all of my classes implement 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...
我想序列化一个Item的数组列表,但它不起作用....
我的Item类扩展了Stuff类并有一些子类。
我所有的类都实现了可序列化。
我有这部分:
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) { }
我的课程 :
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,...)
我认为信息足够了...
it's just a little mistake and now works correctly... I'm sorry for my stupid question.
这只是一个小错误,现在可以正常工作......我很抱歉我的愚蠢问题。
Thank you for your answers.
谢谢您的回答。
采纳答案by Sardor I.
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) {
}
回答by Christien Lomax
I'm taking a leap here that you are trying to serialize to something like json?
我在这里进行了一次飞跃,您正在尝试将其序列化为 json 之类的内容?
if so, you can use Hymanson (http://Hymanson.codehaus.org/)
如果是这样,您可以使用 Hymanson ( http://Hymanson.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 Hymanson as well...
您也可以使用 Hymanson 反序列化...
A more detailed example here: http://blog.inflinx.com/2012/05/10/using-Hymanson-for-javajson-conversion/
这里有一个更详细的例子:http: //blog.inflinx.com/2012/05/10/using-Hymanson-for-javajson-conversion/
Note: you can do similar for XML.
注意:您可以对 XML 执行类似操作。