【问题标题】:How to Store ArrayList Of Custom Objects (Geofence)如何存储自定义对象的 ArrayList(地理围栏)
【发布时间】:2014-07-15 06:27:08
【问题描述】:

我正在尝试使用 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}]

我的实际地理围栏数组列表是:

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

【问题讨论】:

  • write 反序列化泛型集合的方法:
  • 那是一个不同的故事,我什至无法正确存储它。
  • 所以检查你解析 json 的地方,你得到了什么Log.i(TAG, "Json Geofence Objects: " + json);
  • 我已经更新了我的问题,你可以看到我的logcat

标签: java android gson android-geofence


【解决方案1】:

不要为此使用共享偏好。它不是为此而设计的,共享偏好的目的不是存储所有内容。您已经将它保存为 JSON,只需将 json 保存为文件

【讨论】:

  • 我知道将所有内容存储在 sharedpreferences 中是一种不好的做法,但您可以看到即使 json 也不如预期的那样正确。
  • 但是,如果您已经拥有 json 格式的数据(甚至是损坏的 json)并且可以这样解析,为什么不直接使用这种格式呢?除了写入文件之外,所有代码都已写入。
  • 是的,你是对的..暂时我可以做到。谢谢
【解决方案2】:

编辑该行: 字符串 json = gson.toJson(listOfGeofence.mGeofencesList); 经过: String json = gson.toJson(listOfGeofence.getmGeofencesList());

【讨论】:

    猜你喜欢
    • 2013-03-02
    • 1970-01-01
    • 1970-01-01
    • 2015-11-26
    • 2013-12-25
    • 2011-12-26
    • 1970-01-01
    • 1970-01-01
    • 2017-08-01
    相关资源
    最近更新 更多