android - 将哈希表序列化为android上的文件

标签 android serialization hashtable

所以我对序列化场景很陌生,我不知道是否可以序列化哈希表然后将其保存到文件中......但这是我迄今为止尝试过的......对于一些为什么它转到我的代码的 catch 部分而不是执行 Try block ?

public void addDataIntoFlashCardFile(Context context, Hashtable<Integer, ArrayList<Deck>> data) {
        try {
            FileOutputStream fos = context.openFileOutput(
                    FLASHCARDS_FILENAME, Context.MODE_PRIVATE
                            | Context.MODE_APPEND);
            ObjectOutputStream osw = new ObjectOutputStream(fos);
            osw.writeObject(data);

    } catch (FileNotFoundException e) {
        // catch errors opening file
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
        Toast.makeText(context, "calles", Toast.LENGTH_SHORT).show();
    }
    }

在这里,我尝试从文件中读取它(这不会工作,因为它首先不会写入文件)

try {

            Hashtable<Integer, ArrayList<Deck>> temp = new Hashtable<Integer, ArrayList<Deck>>();
            FileInputStream myIn = context.openFileInput(FLASHCARDS_FILENAME);
            ObjectInputStream IS = new ObjectInputStream(myIn);
            Toast.makeText(context, "here", Toast.LENGTH_SHORT).show();
            try {

                temp = (Hashtable<Integer, ArrayList<Deck>>)IS.readObject();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();

            }
            IS.close();

            //testing purposes
            for (int i = 0; i < temp.size(); i++) {
                for (int p = 0; p < temp.get(i).size(); p++) {
                    for (int q = 0; q < temp.get(i).get(p).getDeck().size(); q++) {
                    Toast.makeText(context, temp.get(i).get(p).getDeck().get(q).getQuestion(), Toast.LENGTH_LONG).show();
                    }
                }
            }
        }
        catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(context, "here", Toast.LENGTH_SHORT).show();
        }
    }

最佳答案

您实际上可以使用 JSONObject。

第 1 步:创建您的 JSONObject:

JSONObject myAwesomeObject = new JSONObject();
JSONArray myAwesomeArray = new JSONArray();

第 2 步:遍历 HashMap 并将其添加到 JSONObject

for (Entry<Integer, ArrayList<Deck>> entry : map.entrySet())
{
    ArrayList<Deck> decks = temp.get(entry);
    JSONObject JSONDeck = new JSONObject();
    for (Deck deck : decks){
        JSONDeck.add("deck", deck.getWhateverDataDeckContains());
    }
    myAwesomeArray.add("deck", JSONDeck);
}
myAwesomeObject.add("deck_collection", myAwesomeArray);

第 3 步:获取包含您的表格的字符串:

String myAwesomeContents = myAwesomeObject.toString();

第 4 步:将其作为纯文本插入到文件中。

要反序列化它,您只需遍历 JSONObject 并填充一个新表。

第 1 步:从文件中取回字符串。

第 2 步:创建包含该字符串的 JSONObject 的新实例:

JSONObject deserialized = new JSONObject(stringContainingYourDataInJSON);

第 3 步:取回您的简单数据:

ArrayList<Deck> decksArray = new ArrayList<Deck>();
JSONArray decks = deserialized.getJSONArray("deck_collection");
for (int i=0; i<decks.length(); i++){
    JSONObject deck = decks.get(i);
    String name = deck.getString("name");
    int length = deck.getInt("length");
    // etc
    Deck deckPojo = new Deck();
    deckPojo.setWhatever(whateverParamsYouWantToSet);
    decksArray.add(deckPojo);
 }

关于android - 将哈希表序列化为android上的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13265877/

相关文章:

java - 如何将按钮对齐到选项卡内的中心?

android - LiveData 和 MutableLiveData 的区别

java - 使用 Jackson 序列化空值

python - 为双散列哈希表大小选择最佳素数?

java - 在单线程环境中使用Hashtable等线程安全集合类有什么缺点吗?

android - Jetpack compose NavHost 防止重新组合屏幕

android - BuildConfig 未正确创建(Gradle Android)

java - 如何为 kafka-mirror-maker 创建自定义序列化程序?

ruby-on-rails - Rails "try"常量

algorithm - rehash 时如何提高性能?