简体   繁体   English

为什么我的 LinkedHashMap 被错误地转换为 JSON 并保存在 SharedPreferences 中?

[英]Why is my LinkedHashMap being incorrectly converted to JSON and saved in SharedPreferences?

I'm trying to save an object in SharedPreferences but first converting the object to JSON as shown below.我正在尝试在 SharedPreferences 中保存 object 但首先将 object 转换为 JSON ,如下所示。 However, the object is being both incorrectly converted and saved.但是,object 的转换和保存均不正确。

    private void saveHobbySchedule() {
        String jsonString = new Gson().toJson(hobbySchedule);
        Log.e("Saving Hobby Schedule: ", jsonString);
        SharedPreferences sharedPreferences = getSharedPreferences("UserData", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("hobbySchedule", jsonString);
        editor.apply();
    }

The JSON looks like this: JSON 看起来像这样:

E/Saving Hobby Schedule:: {"com.example.hobbie.ObjectModels.Hobby@a64126":["MONDAY @ 15:36","WEDNESDAY @ 15:36","THURSDAY @ 15:36"]}

This makes it impossible to save and retrieve the hobbySchedule variable.这使得无法保存和检索hobbySchedule变量。

If it helps, hobbySchedule is declared and initialized like this:如果有帮助, hobbySchedule会像这样声明和初始化:

LinkedHashMap<Hobby, ArrayList<String>> hobbySchedule = new LinkedHashMap<>();

Here's the code for the Hobby class as well.这里也是爱好 class 的代码。

public class Hobby implements Serializable {
    private final String hobbyName;
    private final String hobbyEmoji;
    private final String hobbyCategory;
    private final String hobbyDescription;
    private final String hobbyShortDescription;
    private final String hobbyPracticeLocation;
    private final String hobbyImageURL;

    public Hobby() {
        this.hobbyName = "";
        this.hobbyEmoji = "";
        this.hobbyCategory = "";
        this.hobbyDescription = "";
        this.hobbyShortDescription = "";
        this.hobbyPracticeLocation = "";
        this.hobbyImageURL = "";
    }

    public Hobby(String hobbyName, String hobbyEmoji, String hobbyCategory, String hobbyDescription,
                 String hobbyShortDescription, String hobbyPracticeLocation, String hobbyImageURL) {
        this.hobbyName = hobbyName;
        this.hobbyEmoji = hobbyEmoji;
        this.hobbyCategory = hobbyCategory;
        this.hobbyDescription = hobbyDescription;
        this.hobbyShortDescription = hobbyShortDescription;
        this.hobbyPracticeLocation = hobbyPracticeLocation;
        this.hobbyImageURL = hobbyImageURL;
    }

Why is the name of the Hobby file address being converted into JSON and saved instead of the values?为什么将Hobby文件地址的名称转换为JSON并保存而不是值? Please help, thank you!请帮忙,谢谢!

JSON only supports strings as JSON object property names. JSON 仅支持字符串作为 JSON object 属性名称。 Gson therefore by default calls the toString() method to convert non-String Map keys to strings. Gson 因此默认调用toString()方法将非字符串Map键转换为字符串。

If instances of your Hobby class can easily be represented as string, you could override its toString() method.如果您的Hobby class 的实例可以轻松地表示为字符串,您可以覆盖其toString()方法。 For deserialization you will however likely have to register a custom TypeAdapter .但是,对于反序列化,您可能必须注册一个自定义TypeAdapter

Otherwise you can uses GsonBuilder.enableComplexMapKeySerialization() which will serialize your Map as JSON array.否则,您可以使用GsonBuilder.enableComplexMapKeySerialization()它将您的Map序列化为 JSON 数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM