首先说说怎样把j对象数组转换为json字符串,用gson两句话搞定。
Gson gson=new Gson();
String friendlistresult=gson.toJson(friendList);
接下来,要把json字符串转为对象数组则稍微麻烦一些。
Gson gson=new Gson();
List<FriendInfo> tempList=new ArrayList<>();
tempList = gson.fromJson(spContactList.getString("friendList",""), new TypeToken<List<FriendInfo>>(){}.getType());
for(int i=0;i<tempList.size();i++){
FriendInfo friendInfo=tempList.get(i);
friendList.add(friendInfo);
}
刚开始,我以为tempList可以直接当对象数组用,但发现用来构建listview没有数据,然后tempList.get(0). getFriendname ()又有值,所以我循环把tempList的对象数组取出来,放到FriendList中,结果数据果然回来了