I would like to append data into existing JSON object and save data as string into shared preferences with following structure.
"results":[
{
"lat":"value",
"lon":"value"
},
{
"lat":"value",
"lon":"value"
}
]
How can i do it right?
I tried something like this, but without luck.
// get stored JSON object with saved positions
String jsonDataString = this.getSharedPreferencesStringValue(ctx, "positions", "last_positions");
if (jsonDataString != null) {
Log.i(AppHelper.APP_LOG_NAMESPACE, "JSON DATA " + jsonDataString);
JSONObject jsonData = new JSONObject(jsonDataString);
jsonData.put("lat", lat.toString());
jsonData.put("lon", lon.toString());
jsonData.put("city", city);
jsonData.put("street", street);
jsonData.put("date", appHelper.getActualDateTime());
jsonData.put("time", appHelper.getActualDateTime());
this.setSharedPreferencesStringValue(ctx, "positions", "last_positions", jsonData.toString());
} else {
this.setSharedPreferencesStringValue(ctx, "positions", "last_positions","{}");
}
Thanks for any advice.