I need to convert a Vector<Vector<Float>>
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.
I need to convert a Vector<Vector<Float>>
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<Vector<Vector<Float>>>() {}.getType();
Storage:
Vector<Vector<Float>> data = new Vector<Vector<Float>>();
data.add(new Vector<Float>());
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<Vector<Float>> data = new Gson().fromJson(dataAsJson, DATA_TYPE);
Disclaimer: I've never developed for Android.