2

I have an OBJECT ARRAY, how can i serialize this array? I give you c# code and i have to write in java. Thanks. C#:

byte[] data = JSon.Serialize<object[]>(Parameters);

Please note that i want to serialize an array which has objects.. i realize that is not the exact answer:

ObjectOutputStream out = new ObjectOutputStream(anyOutputStream);
out.writeObject(objectArray);

i have a problem with anyOutputStream, i really don't know what i should place on parameter..

patronaviera
  • 35
  • 1
  • 7
  • I think it's already answered here: http://stackoverflow.com/questions/4262390/how-to-serialize-json-object-in-java – DaMachk Jul 31 '13 at 10:12
  • Use any of the existing JSON API's, for example [Jackson](http://wiki.fasterxml.com/JacksonInFiveMinutes) – fvu Jul 31 '13 at 10:14
  • Overview of JSON API's for Java [here](http://stackoverflow.com/questions/338586/a-better-java-json-library/) – fvu Jul 31 '13 at 10:16

2 Answers2

4

If the object array contains Serializable objects then you can use java.io.ObjectOutputStream

ObjectOutputStream out = new ObjectOutputStream(anyOutputStream);
out.writeObject(objectArray);
...
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
1

new JSONObject(parameters).toString().getBytes()

requires JSONObject

David Xu
  • 5,555
  • 3
  • 28
  • 50
  • i have this error message: The constructor JSONObject(Object[]) is undefined, any idea? – patronaviera Jul 31 '13 at 10:34
  • Clearly, you need a `JSONArray` if you're going to serialize an array. However, there's no guarantee it will work---it all depends on what the framework can do with your array elements. – Marko Topolnik Jul 31 '13 at 11:25