I need to convert a Vector> to a JSONArray. Apart from iterating through the vector and creating the JSONArray, is there any simpler way to do this?
Someone told me to try gson.
解决方案
SharedPreferences is just a key-value store. What's stopping you from bypassing JSONObject completely, and just using something like this (Gson only)?
private static final Type DATA_TYPE =
new TypeToken>>() {}.getType();
Storage:
Vector> data = new Vector>();
data.add(new Vector());
data.get(0).add(3.0f);
String dataAsJson = new Gson().toJson(data, DATA_TYPE);
sharedPreferences.edit().putString("data", dataAsJson).commit();
Retrieval:
String dataAsJson = sharedPreferences.getString("data", "[]");
Vector> data = new Gson().fromJson(dataAsJson, DATA_TYPE);
Disclaimer: I've never developed for Android.