JSONObject 不可序列化?

发布于 2024-11-05 07:09:10 字数 4251 浏览 1 评论 0原文

我正在尝试序列化 JSONObjects 的 ArrayList。但我收到错误:

05-07 01:04:24.130: WARN/System.err(913): java.io.NotSerializableException: org.json.JSONObject
05-07 01:04:24.130: WARN/System.err(913):     at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1535)
05-07 01:04:24.130: WARN/System.err(913):     at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1847)
05-07 01:04:24.130: WARN/System.err(913):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1689)
05-07 01:04:24.130: WARN/System.err(913):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1653)
05-07 01:04:24.130: WARN/System.err(913):     at java.util.ArrayList.writeObject(ArrayList.java:651)
05-07 01:04:24.130: WARN/System.err(913):     at java.lang.reflect.Method.invokeNative(Native Method)
05-07 01:04:24.130: WARN/System.err(913):     at java.lang.reflect.Method.invoke(Method.java:507)
05-07 01:04:24.130: WARN/System.err(913):     at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1219)
05-07 01:04:24.130: WARN/System.err(913):     at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1575)
05-07 01:04:24.130: WARN/System.err(913):     at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1847)
05-07 01:04:24.130: WARN/System.err(913):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1689)
05-07 01:04:24.130: WARN/System.err(913):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1653)
05-07 01:04:24.130: WARN/System.err(913):     at org.stocktwits.activity.AddStocksActivity.serializeQuotes(AddStocksActivity.java:183)
05-07 01:04:24.130: WARN/System.err(913):     at org.stocktwits.activity.AddStocksActivity.access$9(AddStocksActivity.java:178)
05-07 01:04:24.130: WARN/System.err(913):     at org.stocktwits.activity.AddStocksActivity$2.onItemClick(AddStocksActivity.java:146)
05-07 01:04:24.130: WARN/System.err(913):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)
05-07 01:04:24.130: WARN/System.err(913):     at android.widget.ListView.performItemClick(ListView.java:3513)
05-07 01:04:24.130: WARN/System.err(913):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
05-07 01:04:24.130: WARN/System.err(913):     at android.os.Handler.handleCallback(Handler.java:587)
05-07 01:04:24.130: WARN/System.err(913):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-07 01:04:24.130: WARN/System.err(913):     at android.os.Looper.loop(Looper.java:130)
05-07 01:04:24.130: WARN/System.err(913):     at android.app.ActivityThread.main(ActivityThread.java:3683)
05-07 01:04:24.130: WARN/System.err(913):     at java.lang.reflect.Method.invokeNative(Native Method)
05-07 01:04:24.130: WARN/System.err(913):     at java.lang.reflect.Method.invoke(Method.java:507)
05-07 01:04:24.130: WARN/System.err(913):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-07 01:04:24.130: WARN/System.err(913):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-07 01:04:24.130: WARN/System.err(913):     at dalvik.system.NativeStart.main(Native Method)

这是我的序列化 &反序列化代码:

private void serializeQuotes() {
        FileOutputStream fos;
        try {
            fos = openFileOutput(Constants.FILENAME, Context.MODE_PRIVATE);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(quotes);
            oos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings("unchecked")
    private void deserializeQuotes() {
        try {
            FileInputStream fis = openFileInput(Constants.FILENAME);
            ObjectInputStream ois = new ObjectInputStream(fis);
            quotes = (ArrayList<JSONObject>) ois.readObject();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

I am trying to serialize an ArrayList of JSONObjects. But I get the error:

05-07 01:04:24.130: WARN/System.err(913): java.io.NotSerializableException: org.json.JSONObject
05-07 01:04:24.130: WARN/System.err(913):     at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1535)
05-07 01:04:24.130: WARN/System.err(913):     at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1847)
05-07 01:04:24.130: WARN/System.err(913):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1689)
05-07 01:04:24.130: WARN/System.err(913):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1653)
05-07 01:04:24.130: WARN/System.err(913):     at java.util.ArrayList.writeObject(ArrayList.java:651)
05-07 01:04:24.130: WARN/System.err(913):     at java.lang.reflect.Method.invokeNative(Native Method)
05-07 01:04:24.130: WARN/System.err(913):     at java.lang.reflect.Method.invoke(Method.java:507)
05-07 01:04:24.130: WARN/System.err(913):     at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1219)
05-07 01:04:24.130: WARN/System.err(913):     at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1575)
05-07 01:04:24.130: WARN/System.err(913):     at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1847)
05-07 01:04:24.130: WARN/System.err(913):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1689)
05-07 01:04:24.130: WARN/System.err(913):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1653)
05-07 01:04:24.130: WARN/System.err(913):     at org.stocktwits.activity.AddStocksActivity.serializeQuotes(AddStocksActivity.java:183)
05-07 01:04:24.130: WARN/System.err(913):     at org.stocktwits.activity.AddStocksActivity.access$9(AddStocksActivity.java:178)
05-07 01:04:24.130: WARN/System.err(913):     at org.stocktwits.activity.AddStocksActivity$2.onItemClick(AddStocksActivity.java:146)
05-07 01:04:24.130: WARN/System.err(913):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)
05-07 01:04:24.130: WARN/System.err(913):     at android.widget.ListView.performItemClick(ListView.java:3513)
05-07 01:04:24.130: WARN/System.err(913):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
05-07 01:04:24.130: WARN/System.err(913):     at android.os.Handler.handleCallback(Handler.java:587)
05-07 01:04:24.130: WARN/System.err(913):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-07 01:04:24.130: WARN/System.err(913):     at android.os.Looper.loop(Looper.java:130)
05-07 01:04:24.130: WARN/System.err(913):     at android.app.ActivityThread.main(ActivityThread.java:3683)
05-07 01:04:24.130: WARN/System.err(913):     at java.lang.reflect.Method.invokeNative(Native Method)
05-07 01:04:24.130: WARN/System.err(913):     at java.lang.reflect.Method.invoke(Method.java:507)
05-07 01:04:24.130: WARN/System.err(913):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-07 01:04:24.130: WARN/System.err(913):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-07 01:04:24.130: WARN/System.err(913):     at dalvik.system.NativeStart.main(Native Method)

Here is my serialize & deserialize code:

private void serializeQuotes() {
        FileOutputStream fos;
        try {
            fos = openFileOutput(Constants.FILENAME, Context.MODE_PRIVATE);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(quotes);
            oos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings("unchecked")
    private void deserializeQuotes() {
        try {
            FileInputStream fis = openFileInput(Constants.FILENAME);
            ObjectInputStream ois = new ObjectInputStream(fis);
            quotes = (ArrayList<JSONObject>) ois.readObject();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

暮凉 2024-11-12 07:09:10

老兄,这是 JSON。为什么不将其序列化为 JSON,而不是将其序列化为 JSONObject 类型的 Java 对象?

例如:

String myJsonObjectSerialized = myJsonObject.toString();

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();

泪意 2024-11-12 07:09:10

如果您仍然希望 Java 内置序列化,而不必将 JSON 对象编组为字符串表示法,您可以做的一件事是从 org.json 扩展 JSONObject 和 JSONArray 并实现 Serialized。

然后,您可以全面使用您自己的 JSONObject 和 JSONArray 版本,而不是原始版本。

确保在子类上定义所有构造函数并调用其 super() 对应项,并实现从属性返回父类型的特定方法,例如 getJSONObject() 和 getJSONArray() 。

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.

浪漫之都 2024-11-12 07:09:10

另一种方法是将 org.json 库替换为 json-simple 库。

json-simple 中的 JSONObject 类扩展了 HashMap,它是 java Serialized 以及更多 java-ish API。

Maven 依赖项:

http://mvnrepository.com/artifact /com.googlecode.json-simple/json-simple/1.1.1

示例用法:

https://www.mkyong.com/java/json-simple-example-read-and-write-json/

Another way is to swap org.json library with json-simple.

The JSONObject class from json-simple extends HashMap which is java Serializable as well as more java-ish API.

Maven dependency:

http://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple/1.1.1

Example usage:

https://www.mkyong.com/java/json-simple-example-read-and-write-json/

冰火雁神 2024-11-12 07:09:10

如果 Serialized 对象类中有 JSONArray,请尝试以这种方式初始化 JSONArray。

私有瞬态 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;  
     }  

 }

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;  
     }  

 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文