JSONObject Not Serializable? JSONObject Not Serializable? json json

JSONObject Not Serializable?


Dude, it's JSON. Why not just serialize it as JSON, instead of as a Java object of type JSONObject?

For example:

String myJsonObjectSerialized = myJsonObject.toString();


In the case you'd still want Java built-in serialization without having to resort to marshal your JSON object into string notation, one thing you could do is extend JSONObject and JSONArray from org.json and just implement Serializable.

Then you can use your own versions of JSONObject and JSONArray across the board instead of the originals.

Make sure you define all constructors on your subclasses and call their super() counterparts as well as implement specific methods that return the parent types such as getJSONObject() and getJSONArray() from properties.


If you have JSONArray inside Serializable object class try to initialize JSONArray in this way.

private transient JSONArray jsonArray;

public class Person implements Serializable {        private String name;       private transient JSONArray jsonArray;     public String getName() {            return name;       }      public void setName(String name) {            this.name = name;       }    public JSONArray getJsonArray() {            return jsonArray;       }      public void setJsonArray(JSONArray jsonArray) {            this.jsonArray = jsonArray;       }   }