【问题标题】:Want to store a HashMap? [duplicate]想要存储一个 HashMap? [复制]
【发布时间】:2013-06-20 14:06:36
【问题描述】:

我有一个在活动中生成的 HashMap,我想使用类似于 SharedPreferences 的东西来存储它。我试图查找信息并引用了名为 GSON 的东西,但我不确定那是什么。如果我有类似的东西:

HashMap<String, String> hashMap = new HashMap<String, String> ();
hashMap.put("String 1", "Value 1");
hashMap.put("String 2", "Value 2");

如何存储hashMap,以便我可以在另一个活动中读取它并使用该信息?如果用户关闭应用程序,它也需要存储。

【问题讨论】:

标签: java android hashmap gson


【解决方案1】:

Json 与 HasMap 非常相似。要保存 Json,您必须使用 HasMap 之类的键和值:

JSONObject userDetail = new JSONObject();

        userDetail.put("email", email);
        userDetail.put("token", token);

之后,您可以使用 FileWriter 或 FileInputStream 将其保存在文件 .json 中,您可以使用 JSONParser 从其他活动中获取它。

更多关于json的信息请看this

【讨论】:

    【解决方案2】:

    跟着维克多的回答走。

    但是如果你的 hashMap 很复杂,比如(hash inside hash of hash) 您可以将其直接存储在一个文件中,稍后再阅读:

    写入文件:

        public void saveHashToFile(HashMap<String, Object> hash) throws IOException{
            String filePath = getApplicationContext().getFilesDir().getPath().toString() + "/your.properties";
            File file = new File(filePath);
            FileOutputStream f = new FileOutputStream(file);
            ObjectOutputStream s = new ObjectOutputStream(f);
            s.writeObject(getProperty_Global());
            s.close();  
    
    
        }
    

    从文件读回:

          public HashMap<String, Object> getLocalHashFromFile() throws OptionalDataException, ClassNotFoundException, IOException{
    
            String filePath = getApplicationContext().getFilesDir().getPath().toString() + "/your.properties";
            File file = new File(filePath);
            FileInputStream f = new FileInputStream(file);
    
            ObjectInputStream s = new ObjectInputStream(f);
    
            HashMap<String, Object> hashFromFile=(HashMap<String, Object>) s.readObject();
            s.close();
            Log.e("hashfromfileLOCAL", ""+hashFromFile);
    
            return hashFromFile;
        }
    

    【讨论】:

    • 这也是一个好方法,但是如果你想在 hash 中保存 hash,你也可以使用 JSONArray :)
    【解决方案3】:

    Gson 是一个 Java 库,用于将 Java 对象转换为 JSON 字符串,反之亦然。 我在我的项目中遇到了同样的问题,并使用 Gson 将 HashMap 转换为字符串,将其保存到 SharedPreferences 中,然后在另一个活动中将其取回。

    要存储地图:

    SharedPreferences preferences = getSharedPreferences("com.your.package", MODE_PRIVATE);
    Type genericType = new TypeToken<HashMap<String, String>>() {}.getType();
    String serializedHashMap = Helpers.serializeWithJSON(your_hashmap, genericType);
    preferences.edit().putString("Somename", serializedHashMap).commit();
    

    serializeWithJSON():

    public static String serializeWithJSON(Object o, Type genericType) {
        Gson gson = new Gson();
        return gson.toJson(o, genericType);
    }
    

    反序列化:

    Gson gson = new Gson();
    Type genericType = new TypeToken<HashMap<String, String>>() {}.getType();
    HashMap<String, String> hashMap = gson.fromJson(preferences.getString("Somename", "Errormessage"), genericType);
    

    要将其永久存储在文件中,请使用 amalBit 的答案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 2016-12-20
      • 1970-01-01
      • 2012-02-14
      • 1970-01-01
      • 2020-09-24
      相关资源
      最近更新 更多