3

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.

7
  • For What you are saving complete JSON object in SharedPreferences ? Commented May 18, 2014 at 0:04
  • Question is about creating JSON structure. not about purpose of saving JSON string in shared prefs. I think that is it more simplest solution that work with SQL in Android. Commented May 18, 2014 at 6:30
  • Creating JSON structure? How do you mean? Commented May 18, 2014 at 9:12
  • I want to append new object to old object, to get JSON structure like in my question. Commented May 18, 2014 at 9:23
  • You never explained what's going wrong with your code. Commented May 18, 2014 at 22:45

2 Answers 2

8

I think the easier way to achieve that would be using Gson.

If you are using gradle you can add it to your dependencies

compile 'com.google.code.gson:gson:2.2.4'

To use it you need to define classes to the objects you need to load. In your case it would be something like this:

// file MyObject.java
public class MyObject {
    List<Coord> results = new ArrayList<Coord>();    

    public static class Coord {
        public double lat;
        public double lon;
}

Then just use it to go to/from Json whenever you need:

String jsonDataString = this.getSharedPreferencesStringValue(ctx, "positions", "last_positions");
Gson gson = new Gson();
MyObject obj = gson.fromJson(jsonDataString, MyObject.class);
// Noew obj contains your JSON data, so you can manipulate it at your will.
Coord newCoord = new Coord();
newCoord.lat = 34.66;
newCoord.lon = -4.567;
obj.results.add(newCoord);
String newJsonString = gson.toJson(obj);
Sign up to request clarification or add additional context in comments.

Comments

3

SharedPreferences can only store basic type, such as String, integer, long, ... so you can't use it to store objects. However, you can store the Json String, using sharedPreferences.putString()

2 Comments

Yes, im converting created JSON into string before save using jsonData.toString(). Question is about creating JSON structure. not about purpose of saving JSON string in shared prefs.
I don't get it, sorry, you want to edit your JSON object and edit the value in sharedPreferences ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.