我尝试使用 Gson 在 sharedPreferences 中存储地理位置列表。 这是我到目前为止所做的事情
ArrayListOfGeofence listOfGeofence = new ArrayListOfGeofence();
listOfGeofence.setmGeofencesList(mGeofenceList);
editor = goefenceIdsPreferences.edit();
Gson gson = new Gson();
String json = gson.toJson(listOfGeofence.mGeofencesList);
Log.i(TAG, "Json Geofence Objects: " + json);
editor.putString(fenceName, json).commit();
事情不应该是预期的。这就是我的日志显示的内容:
Json Geofence Objects: [{"Jo":"1","NU":3,"Oz":-1,"NX":12.93217,"NY":77.63186,"NZ":300.0,"Oa":0,"Ob":-1,"NW":1,"xH":1}]
我的实际地理围栏ArrayList是:
ArrayListOfGeofence: [Geofence[CIRCLE id:1 transitions:3 12.932490, 77.630830 300m, resp=0s, dwell=-1ms, @-1], Geofence[CIRCLE id:2 transitions:3 12.934540, 77.628650 300m, resp=0s, dwell=-1ms, @-1]]
这就是我为 ArrayListOfGeofence 类
撰写的内容public class ArrayListOfGeofence implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
List<Geofence> mGeofencesList = new ArrayList<Geofence>();
public List<Geofence> getmGeofencesList()
{
return mGeofencesList;
}
public void setmGeofencesList(List<Geofence> mGeofencesList)
{
this.mGeofencesList = mGeofencesList;
}
}
但我无法取回我的Geofence Objects.所以它给了我一个错误:
GSON Generic: failed to deserialized json object
答案 0 :(得分:1)
不要为此使用共享偏好。它不是为它而制造的,共享偏好的目的不是为了存储一切。你已经把它作为JSON,只需将json保存为文件
答案 1 :(得分:0)
编辑该行: String json = gson.toJson(listOfGeofence.mGeofencesList); 通过: String json = gson.toJson(listOfGeofence.getmGeofencesList());