gpt4 book ai didi

java - 如何存储自定义对象的 ArrayList(地理围栏)

转载 作者:行者123 更新时间:2023-11-30 02:47:40 25 4
gpt4 key购买 nike

我正在尝试使用 GsonsharedPreferences 中存储 地理围栏列表。这是我到目前为止所做的

           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;
}

}

但是我无法取回我的地理围栏对象。所以它给了我一个错误:

GSON Generic: failed to deserialized json object

最佳答案

不要为此使用共享首选项。它不是为此而生,共享偏好的目的不是存储所有内容。您已经将其作为 JSON,只需将 json 另存为文件即可

关于java - 如何存储自定义对象的 ArrayList(地理围栏),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24751275/

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