在网上查了一些资料后,精简改写后得出自己想用的形式,记录一下
public static void putHashMapData(Context context, String key, Map
JSONArray mJsonArray = new JSONArray();
Iterator
JSONObject object = new JSONObject();
while (iterator.hasNext()) {
Map.Entry
try {
object.put(entry.getKey(), entry.getValue());
} catch (JSONException e) {
}
}
mJsonArray.put(object);
SharedPreferences sp = context.getSharedPreferences("config",
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString(key, mJsonArray.toString());
editor.commit();
}
public static Map
Map
SharedPreferences sp = context.getSharedPreferences("config",
Context.MODE_PRIVATE);
String result = sp.getString(key, "");
try {
JSONArray array = new JSONArray(result);
for (int i = 0; i < array.length(); i++) {
JSONObject itemObject = array.getJSONObject(i);
JSONArray names = itemObject.names();
if (names != null) {
for (int j = 0; j < names.length(); j++) {
String name = names.getString(j);
String value = itemObject.getString(name);
datas.put(name, value);
}
}
}
} catch (JSONException e) {
}
return datas;
}
发表评论