Java 使用Gson序列化具有非基元值的对象的ArrayList?

Java 使用Gson序列化具有非基元值的对象的ArrayList?,java,android,serialization,gson,sharedpreferences,Java,Android,Serialization,Gson,Sharedpreferences,所以我有这门课 public class Place { ArrayList<Stop> stops; String name; Location location; public Place(String name, Location location){ this.name = name; this.location = location; } public void addStop(Stop

所以我有这门课

public class Place {

    ArrayList<Stop> stops;
    String name;
    Location location;

    public Place(String name, Location location){
        this.name = name;
        this.location = location;
    }

    public void addStop(Stop stop){
        this.stops.add(stop);
    }

    public Location getLocation() {
        return location;
    }

    public String toString(){
        return name+ " : " +location.toString();
    }
}
公共课堂场所{
ArrayList停止;
字符串名;
位置;
公共场所(字符串名称、位置){
this.name=名称;
这个位置=位置;
}
公共无效添加停止(停止停止){
this.stops.add(stop);
}
公共位置getLocation(){
返回位置;
}
公共字符串toString(){
返回name+“:”+location.toString();
}
}
我在Android项目中使用的。每个Place对象都有一个Location对象(来自Google Play Services),应用程序有一个ArrayList items ArrayList存储用户输入数据。我使用Gson序列化该ArrayList并将其保存到SharedPrefs,但是当我恢复数据时,Location对象中的所有参数都为null。这是我的密码:

//If user has defined places, load them from storage
mPrefs = getActivity().getPreferences(MODE_PRIVATE);
Gson gson = new Gson();
String json = mPrefs.getString("user_places", "");
Type placeListType = new TypeToken<ArrayList<Place>>(){}.getType();
if(json.length() > 0) items = gson.fromJson(json, placeListType);
else  items = new ArrayList<Place>();

...

//Saves user data for future use
SharedPreferences.Editor prefsEditor = mPrefs.edit();
Gson gson = new Gson();
String json = gson.toJson(items);
prefsEditor.putString("user_places", json);
prefsEditor.apply();
//如果用户定义了位置,请从存储器中加载它们
mPrefs=getActivity().getPreferences(模式\私有);
Gson Gson=新的Gson();
String json=mPrefs.getString(“用户位置”,“位置”);
类型placeListType=newTypeToken(){}.getType();
if(json.length()>0)items=gson.fromJson(json,placeListType);
else items=new ArrayList();
...
//保存用户数据以备将来使用
SharedReferences.Editor prefsEditor=mPrefs.edit();
Gson Gson=新的Gson();
字符串json=gson.toJson(items);
putString(“用户位置”,json);
prefsEditor.apply();

我做错了什么?如何确保正确还原原始位置对象?

如果我正确理解了您的问题,可能是因为您正在加载和保存来自两个不同活动的Json字符串

SharedPreferences mPrefs = getActivity().getPreferences(MODE_PRIVATE);
getPreferences()方法仅在您访问同一活动中的值时有效

如果要将值保存到活动A中的SharedReferences,并在稍后的活动B中获取这些值,请尝试使用以下方法:

SharedPreferences mPrefs = getSharedPreferences("MySharedPreferences", MODE_PRIVATE);
SharedPreferences.Editor prefsEditor = mPrefs.edit();
Gson gson = new Gson();
String json = gson.toJson(items);
prefsEditor.putString("user_places", json);
prefsEditor.apply();
然后像这样访问它们:

SharedPreferences mPrefs = getActivity().getPreferences(MODE_PRIVATE);
Gson gson = new Gson();
String json = mPrefs.getString("user_places", "");

其实都是在同一个活动中。。。我使用getActivity()而不是“this”,因为这是在片段中发生的。我在将数据保存到SharedRefs或从SharedRefs检索数据方面并没有什么问题。我的问题是,反序列化时,嵌套在Place对象中的Location对象(本身嵌套在我保存的ArrayList中)的参数为空。@Dat8StringGuy好吧,这很奇怪,我建议您在logcat中显示Location对象值,因此,您将知道,可能正在将null作为参数传递给构造函数。也可以考虑创建一个字段字符串位置JSON,将位置转换为构造函数中的JSON(如果以前你检查过的那个位置对象不是构造函数),那么听起来不太好,但是我没有任何其他的想法,它也会发生在1,2个或1000个位置和/或站点?能否显示序列化后收到的
JSON
?我最终改变了
Place
的结构。它不具有
位置
对象,只存储初始
位置
对象的经度/纬度坐标,应用程序在反序列化时重建它。当我测试GSON返回的
JSON
字符串时,我注意到
Location
对象中只有一个参数被序列化(记录
Location
的时间)。所以这可能是序列化的问题,而不是反序列化的问题