Java 无法从SharedReferences检索hashmap
我从Parse中检索ParseObject列表,并将其转换为HashMap列表,然后将其转换为JSONArray,并将其作为字符串存储在SharedReferences中Java 无法从SharedReferences检索hashmap,java,android,json,hashmap,Java,Android,Json,Hashmap,我从Parse中检索ParseObject列表,并将其转换为HashMap列表,然后将其转换为JSONArray,并将其作为字符串存储在SharedReferences中 List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>(); for (int i = 0; i < list.size(); i++)
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();
List data=new ArrayList();
对于(int i=0;i
然后我从本地保存的字符串(JSONArray)中检索
String storedCollection=pref.getString(“类别”,null);
//解析字符串以填充集合。
collection=newarraylist();
if(storedCollection!=null){
试一试{
JSONArray数组=新的JSONArray(storedCollection);
HashMap项=null;
对于(int i=0;i
它在棒棒糖版本和更高版本上运行良好,但在更低版本上出现错误
org.json.JSONException:第21个字符处的未终止数组
{jobs=[酒吧管理、面包师、酒吧服务、咖啡师、停车场服务、,
厨师长,清洁服务,烹饪和食品准备],最新更新=周二
2016年6月28日10:22:03 GMT+05:30,类别=全职}
有时会出现这种错误
java.lang.ClassCastException:org.json.JSONObject无法强制转换为
java.lang.String
问题是存储的数组是HashMaps的JSONArray。检索数组时,数组中的对象是字符串(表示HashMap)
jsonobjectobj=newjsonobject((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 storeInPrefs(){
JSONArray data=新的JSONArray();
对于(int i=0;i
要解析存储的数据并将其放入集合中,您可以这样做
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);
}
}
}
public void parseStoredData(){
SharedPreferences pref=this.getPreferences(MODE_PRIVATE);
String storedCollection=pref.getString(“类别”,null);
//解析字符串以填充集合。
ArrayList集合=新建ArrayList();
if(storedCollection!=null){
试一试{
JSONArray数组=新的JSONArray(storedCollection);
对于(int i=0;i
在我看来,这对您来说很容易。尝试使用JSONObject object=newjsonobject(行业)代码>
而不是JSONArray结果=新的JSONArray(数据)代码>
这个object.toString()
类似于{“作业”:[“酒吧管理”、“面包师”、“酒吧服务”],“最新更新”:“星期二6月28日13:54:24 GMT+2016年8:00”,“类别名称”:“全职”}
而result.toString()
类似于[{“Jobs”:[“酒吧管理”、“面包师”、“酒吧服务”],“LastUpdate:“Tue Jun 28 13:54:24 GMT+08:00 2016”,“CategoryName:“fulltime”}]
我认为对象是您真正想要的,在检索时也使用JsonObject
希望对你有帮助
此外,您还可以使用来验证结果。I
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);
}
}
}