java - 当对象具有 ArrayList 属性时,如何使用 gson 将对象列表转换为 json

标签 java android arraylist gson android-sharedpreferences

当我的应用程序首次启动时,我试图在共享首选项中存储一些对象列表,并且我将在我的应用程序的后续使用中使用此对象列表。 我的类(class):

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

public class MyClass {
    private int a;
    private String b;
    private int c;
    private ArrayList<NameValuePair> d;

    public MyClass(int a, String b, int c) {
        super();
        this.a = a;
        this.b = b;
        this.c = c;

    }

    public MyClass(int a, String b, int c, ArrayList<NameValuePair> d) {
        super();
        this.a = a;
        this.b = b;
        this.c = c;
        this.d = d;
    }

    public int getA() {
        return a;
    }

    public void setA(int a) {
        this.a = a;
    }

    public String getB() {
        return b;
    }

    public void setB(String b) {
        this.b = b;
    }

    public int getC() {
        return c;
    }

    public void setC(int c) {
        this.c = c;
    }

    public ArrayList<NameValuePair> getD() {
        return d;
    }

    public void setD(ArrayList<NameValuePair> d) {
        this.d = d;
    }

    public static List<MyClass> getMyObjects() {
        // TODO Auto-generated method stub
        List<MyClass> myObjects = new ArrayList<MyClass>();

        ArrayList<NameValuePair> temp1 = new ArrayList<NameValuePair>();
        temp1.add(new BasicNameValuePair("1", "20"));
        temp1.add(new BasicNameValuePair("2", "30"));
        temp1.add(new BasicNameValuePair("3", "40"));

        ArrayList<NameValuePair> temp2 = new ArrayList<NameValuePair>();
        temp2.add(new BasicNameValuePair("1", "50"));
        temp2.add(new BasicNameValuePair("2", "60"));
        temp2.add(new BasicNameValuePair("3", "70"));

        ArrayList<NameValuePair> temp3 = new ArrayList<NameValuePair>();
        temp3.add(new BasicNameValuePair("1", "50"));
        temp3.add(new BasicNameValuePair("2", "60"));
        temp3.add(new BasicNameValuePair("3", "70"));

        myObjects.add(new MyClass(1, "ABC", 20, temp1));
        myObjects.add(new MyClass(2, "DEF", 30, temp2));
        myObjects.add(new MyClass(3, "GHI", 40, temp3));

        return myObjects;
    }
}

我使用以下代码行创建了对象列表并将其存储在共享首选项中:

 List<MyClass> myObjects = MyClass.getmyObjects();
    String myObjectsJSONString = new Gson().toJson(myObjects);
    prefsEditor.putString("MYOBJECTS", myObjectsJSONString);

将 myObjects 写入共享首选项时按预期形成的 JSON 字符串 示例:

 [{"d":[{"name":"1","value":"20"},{"name":"2","value":"30"},{"name":"3","value":"40"}],"b":"ABC","c":20,"a":1},{"d":[{"name":"1","value":"50"},{"name":"2","value":"60"},{"name":"3","value":"70"}],"b":"DEF","c":30,"a":2},{"d":[{"name":"1","value":"50"},{"name":"2","value":"60"},{"name":"3","value":"70"}],"b":"GHI","c":40,"a":3}]

在后续使用中,我尝试使用以下代码行填充我的列表:

String myobjectsJSONString =appPrefs.getString("MYOBJECTS", null);
Type type = new TypeToken < List <MyClass>> () {}.getType();
List <MyClass> myObjects = new Gson().fromJson(myobjectsJSONString, type);

我能够从共享首选项存储和生成对象列表 没有属性“d”,其值为 ArrayList<NameValuePair>类型;

但是,使用“d”作为属性之一,我无法生成该对象。

显示的错误是: java.lang.RuntimeException:无法调用接口(interface) org.apache.http.NameValuePair 的无参数构造函数。向 Gson 注册此类型的 InstanceCreator 可能会解决此问题

尝试从共享首选项中存储的 json 填充对象时引发上述异常

最佳答案

我想我现在明白了:您的 MyClass 包含一个字段

private  ArrayList<NameValuePair> d;

NameValuePair 然而,实际上是 Apache 的 org.apache.http.NameValuePair,它是一个接口(interface)。因此,Gson 无法创建它的实例 - 它只能创建类的实例。

你可以尝试将其更改为

private  ArrayList<BasicNameValuePair> d;

但是该类似乎没有无参数构造函数,因此它可能也不起作用(不过,这个答案说它应该: https://stackoverflow.com/a/18645370/763935 )。

因此,最安全的选择可能是编写自己的类来实现 NameValuePair:

public class MyNameValuePair implements NameValuePair {
    private final String name;
    private final String value;

    public MyNameValuePair() {
        this.name = null;
        this.value = null;
    }

    @Override
    public String getName() {
        return this.name;
    }

    @Override
    public String getValue() {
        return this.value;
    }
}

关于java - 当对象具有 ArrayList 属性时,如何使用 gson 将对象列表转换为 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35131250/

相关文章:

java - 无法解析的日期与时区

java - 为什么要避免 Java 中的常量折叠?什么时候?

android - 在 TextView/EditText 上使用 "android:textAppearance"失败,但 "style"有效

java - 构建 ArrayList 树 - Java

java - 如何在 Java 中检查 TCP/IP 服务器是否存在

java - executorService多线程池的性能

android - startActivityForResult 函数中的 PICK_CONTACT 参数是什么?

android - 当用户从 flutter 的设置屏幕导航回应用程序时检测方法

powershell - ArrayList 中的空元素导致 'Cannot bind argument to parameter' 错误

java - 无法调用 Arraylist 上定义的方法