gpt4 book ai didi

java - 无法从 sharedPreferences 检索 HashMap

转载 作者:太空狗 更新时间:2023-10-29 13:57:47 25 4
gpt4 key购买 nike

我正在从 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com