相关疑难解决方法(0)

Gson序列化多态对象列表

我正在尝试使用Gson将涉及多态的对象序列化/反序列化为JSON.

这是我的序列化代码:

ObixBaseObj lobbyObj = new ObixBaseObj();
lobbyObj.setIs("obix:Lobby");

ObixOp batchOp = new ObixOp();
batchOp.setName("batch");
batchOp.setIn("obix:BatchIn");
batchOp.setOut("obix:BatchOut");

lobbyObj.addChild(batchOp);

Gson gson = new Gson();
System.out.println(gson.toJson(lobbyObj));
Run Code Online (Sandbox Code Playgroud)

这是结果:

 {"obix":"obj","is":"obix:Lobby","children":[{"obix":"op","name":"batch"}]}
Run Code Online (Sandbox Code Playgroud)

序列化大多的作品,除了它缺少继承成员的内容(尤其是obix:BatchInobixBatchout字符串丢失).这是我的基类:

public class ObixBaseObj  {
    protected String obix;
    private String display;
    private String displayName;
    private ArrayList<ObixBaseObj> children;

    public ObixBaseObj()
    {
        obix = "obj";
    }

    public void setName(String name) {
        this.name = name;
    }
        ...
}
Run Code Online (Sandbox Code Playgroud)

这是我继承的类(ObixOp)的样子:

public class ObixOp extends ObixBaseObj {
    private String in;
    private String out;

    public ObixOp() { …
Run Code Online (Sandbox Code Playgroud)

java serialization json gson

42
推荐指数
3
解决办法
3万
查看次数

如何使用接口序列化类?

我从未对序列化做过多少工作,但我尝试使用Google的gson将Java对象序列化为文件.这是我的问题的一个例子:

public interface Animal {
    public String getName();
}


 public class Cat implements Animal {

    private String mName = "Cat";
    private String mHabbit = "Playing with yarn";

    public String getName() {
        return mName;
    }

    public void setName(String pName) {
        mName = pName;
    }

    public String getHabbit() {
        return mHabbit;
    }

    public void setHabbit(String pHabbit) {
        mHabbit = pHabbit;
    }

}

public class Exhibit {

    private String mDescription;
    private Animal mAnimal;

    public Exhibit() {
        mDescription = "This is a …
Run Code Online (Sandbox Code Playgroud)

java json interface gson

34
推荐指数
2
解决办法
4万
查看次数

无法为 Product.class 调用无参数构造函数

我在我的 android 应用程序中使用 GSON 和 Volley 库进行网络连接,但是在使用 Gson 将 Json 响应转换为模型类时,我得到了以下错误:

88-2006/ E/Volley? [121] NetworkDispatcher.run: Unhandled exception java.lang.RuntimeException: Unable to invoke no-args constructor for class [Lcom.example.model.Product;. Register an InstanceCreator with Gson for this type may fix this problem.
    java.lang.RuntimeException: Unable to invoke no-args constructor for class [Lcom.example.model.Product;. Register an InstanceCreator with Gson for this type may fix this problem.
Run Code Online (Sandbox Code Playgroud)

这些是我正在使用的 POJO 类:Product.java

public class Product {

    private String status;

    private List<Result> results = new ArrayList<Result>();

    private Pagination pagination;
    public Product(){} …
Run Code Online (Sandbox Code Playgroud)

java android getjson gson

5
推荐指数
1
解决办法
2万
查看次数

Android 数组与 Gson 的 Sharedpreferences

我需要创建一个包含最近 5 个通话日期的数组。

我知道我必须将日期保存在数组中,但我不知道如何重新排序其他记录以将最后一次调用日期保留为第一个记录。并且始终只维护5条记录

目标是保存最后一个调用字符串并添加到数组中,之后我重新排序并仅维护 5 条记录,之后我使用 FlexJson 制作一个字符串并保存在共享首选项上。谁有这个完整的流程吗?

这是我正在做的事情,但到处都抛出错误:

节省

SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss dd-MM-yyyy", Locale.getDefault());
Calendar calendar = Calendar.getInstance();
String currentDateTimeString = df.format(calendar.getTime());

List<String> textList = new ArrayList<>();
JSONSerializer ser = new JSONSerializer();
textList.add(currentDateTimeString);

String jsonText = ser.deepSerialize(textList);

editor.putString("lastCall", jsonText);
editor.commit();
Run Code Online (Sandbox Code Playgroud)

读:

String lastCall = callLogPreferences.getString("lastCall", null);
JSONDeserializer<List<String>> der = new JSONDeserializer<>();
List<String> textList = der.deserialize(lastCall);
Run Code Online (Sandbox Code Playgroud)

我正在使用 FlexJson:GSON 和 InstanceCreator 问题

它没有从字符串转换为数组列表

java arrays android sharedpreferences flexjson

2
推荐指数
1
解决办法
2612
查看次数