LibGDX JSON : Changing data and writing to file on Android Device

I’ve been looking into using JSON as a way to store data for a puzzle game I’m working on.

My JSON data looks like this:

{
   "paintingsData": [
      {
         "hashmap_key": "gulf",
         "painter": "Josephus Augustus Knip",		 
         "title": "The Gulf of Naples with the Island of Ischia in the Distance",
         "year": "c. 1818",
         "filepath_painting": "paintings/gulf_of_naples1280x785_q5.jpg",
         "filepath_button": "select_screen/gulf_of_naples.png",
         "button_text": "The Gulf of Naples",
         "puzzle_state": "play"
      },
...
... ec. times ~50

Now i figured out how to get all the data out of my json file and create an ArrayList of PaintingData objects that I can use throughout my game.
That’s all good, but what I’m looking to get a little help with are 2 things. (I have been looking at some tutorials and at the LibGDX wiki but so far haven’t figured it out):

1. How do I change data in my JSON file. For example if the user completes a puzzle I want to change the “puzzle_state” from “play” to “solved”.

2. How do I store my changed data from my changed json file on Android. I know I can save data to a file something like:


String string= json.prettyPrint(arrayList-that-contaise-the-JSON-data);
FileHandle file = Gdx.files.external("jsontest.json");
file.writeString(string, false);

This will create a jsontest.json in the root of my libgdx desktop project, but that liekly won’t work on an Android Device.
Having read: http://developer.android.com/training/basics/data-storage/files.html it looks to me that I want to save the file on InternalStorage, but I have no idea on how to do that yet.

Any help?

I wont recommend saving those user individuall stats in the level discription.
Image an update that adds more levels. Shall that override the actual user progress?

Create a ‘Save-File’ for every user (1 ?) and save those stats.
For simple things you could look Preferences up. https://github.com/libgdx/libgdx/wiki/Preferences
For more complicated you could take the actual approach.

-ClaasJG

Yes, I was thinking about using preferences as well & with keeping in mind possible updates it seems like the smarter way to go.
But I´m still curious about how to change the json file and store it when the game is running on an Android device. I like to get more knowledgeable

Supposed it is a resource in the jar/ (project (assets)) it is not possible.
You could load it as object tree, set the things you want to set and store it either in the internal storage http://developer.android.com/guide/topics/data/data-storage.html#filesInternal or the external storage http://developer.android.com/guide/topics/data/data-storage.html#filesExternal
-ClaasJG

I think he is asking for how to perform an operation like this:


JSONObject json = new JSONObject();

json.put("name", "JavaGaming");
json.put("age", 10);
json.put("image", "/c/image.png");

String jsonString = json.toString();

But from my experience Libgdx json implementation doesn’t support that.

@trollwarrior:
No, I’ve already got all the data stored in a json file so I don’t want to create a json object and do key-value pairing (json.put…) from within my code, but I would like to know how I could best approach changing data from my existing JSON file (and - the most important part - then storing it again on Android).
I guess since I’m parsing all the data from the JSON file into Objects in an ArrayList I can check the arrayList for the object with the specific key I’m looking for and then change the “puzzle_state” value. and then writing the ArrayList back to file again like this:


String jsonString = json.toJson(ArrayList );
FileHandle file = Gdx.files.local("jsontest.json");
file.writeString(jsonString , false);

Or if I want it to be nicely formatted …


String jsonString = json.prettyPrint(ArrayList );

But I was wondering if there is a way to directly itterate through the Json file (instead of find the specific object in the ArrayList with the parsed JSON data, change some of its values, store data from the ArrayList in a string and overwrite to original json file with that string’s data), find the object with the specific key value I want and change just the value.

I’m not really sure what you mean… I don’t see how would passing ArrayList to generate json string would make you a valid json.