java - 无法从 sharedPreferences 检索 HashMap

标签 java android json hashmap

我正在从 Parse 中检索 ParseObject 列表并将其转换为 HashMap 列表,然后将其转换为 JSONArray 并作为字符串存储在 SharedPrefrences 中

List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
                    for (int i = 0; i < list.size(); i++) {
                        ParseObject foundObject = list.get(i);
                        HashMap<String, Object> industry = new HashMap<String, Object>();
                        industry.put("CategoryName", foundObject.get("category"));
                        industry.put("lastUpdated", new Date());
                        industry.put("Jobs", foundObject.getList("jobs"));
                        data.add(industry);
                    }
                        JSONArray result = new JSONArray(data);
                        editor.putString("Categories", result.toString());
                        editor.commit();

然后我从本地保存的 String(JSONArray) 中检索

String storedCollection = pref.getString("Categories", null);
        //Parse the string to populate your collection.
        collection = new ArrayList<HashMap<String, Object>>();
        if (storedCollection != null) {
            try {
                JSONArray array = new JSONArray(storedCollection);

                HashMap<String, Object> item = null;
                for (int i = 0; i < array.length(); i++) {
                    try {

                        JSONObject obj = new JSONObject((String) array.get(i));


                        Iterator<String> it = obj.keys();

                        item = new HashMap<String, Object>();
                        while (it.hasNext()) {
                            String key = it.next();
                            item.put(key, obj.get(key));
                        }
                        if (item == null) {
                            JSONObject obj2 = (JSONObject) array.get(i);
                            Iterator<String> it1 = obj2.keys();
                            item = new HashMap<String, Object>();
                            while (it1.hasNext()) {
                                String key = it.next();
                                item.put(key, obj2.get(key));
                            }
                        }
                    } catch (JSONException e) {

                    }
                    collection.add(item);
                }

            } catch (JSONException e) {
                Log.e("JSON", "while parsing", e);
            }
        }

在 lollipop 版本及更高版本上工作正常,但在较低版本上出错

org.json.JSONException: Unterminated array at character 21 of {jobs=[Bar management, Baker, Bar service, Barista, Car park services, Chef, Cleaning services, Cooking & food preparation], lastUpdated=Tue Jun 28 10:22:03 GMT+05:30 2016, category=fulltime}

有时会出现这个错误

java.lang.ClassCastException: org.json.JSONObject cannot be cast to java.lang.String

最佳答案

问题是你存储的数组是 HashMaps 的 JSONArray。检索数组时,数组中的对象是字符串(代表HashMap)。 JSONObject obj = new JSONObject((String) array.get(i)); 您正在尝试将其转换为 JSONObject。这就是问题所在。要么将每个字符串转换回 hashmap,要么像这样使用 JSONObject 代替 HashMap 来存储数据

public void storeInPrefs(){
    JSONArray data = new JSONArray();
    for (int i = 0; i < list.size(); i++) {

        JSONObject industry = new JSONObject();
        ParseObject foundObject = list.get(i);
        try {
            industry.put("CategoryName", foundObject.get("category"));
            industry.put("lastUpdated", new Date());
            industry.put("Jobs", foundObject.getList("jobs"));
            data.put(industry);
        }
        catch (JSONException e) {
            e.printStackTrace();
        }
    }
    SharedPreferences preferences = this.getPreferences(MODE_PRIVATE);
    SharedPreferences.Editor editor = preferences.edit();
    editor.putString("Categories", data.toString());
    editor.commit();
}

并且要解析存储的数据并将其放入集合中,你可以这样做

public void parseStoredData(){
    SharedPreferences pref = this.getPreferences(MODE_PRIVATE);
    String storedCollection = pref.getString("Categories", null);
    //Parse the string to populate your collection.
    ArrayList<HashMap<String, Object>> collection = new ArrayList<HashMap<String, Object>>();
    if (storedCollection != null) {
        try {

            JSONArray array = new JSONArray(storedCollection);

            for (int i = 0; i < array.length(); i++) {
                try {

                    JSONObject object = array.getJSONObject(i);
                    HashMap<String,Object> item = new HashMap<String, Object>();
                    Iterator<String> it = object.keys();

                    while (it.hasNext()) {
                        String key = it.next();
                        item.put(key, object.get(key));
                    }

                    collection.add(item);
                } catch (JSONException e) {
                        e.printStackTrace();
                }

            }

        } catch (JSONException e) {
            Log.e("JSON", "while parsing", e);
        }
    }
}

我认为这对您来说很容易。

关于java - 无法从 sharedPreferences 检索 HashMap ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38067358/

相关文章:

java - 具有多个参数的 K-means 算法

java - 泛型:为什么我不能将 Object 作为我的 Collection 的参数?

java - Spring Jedis 获取键列表返回空

android - 从 Fragment 更改 ActionBar 标题

android - 无法在 FutureBuilder 中滚动 ListView

javascript - 图表 js 图表条形图不显示从 0 开始的数据

java - 如何从 http 服务器返回 session ID

android - 在 Android 10 上从 contentProvider 加载所有图像

java - 如何用java语言关键字解析json

json - json 解析器的 Apache Spark 对象不可序列化异常