Search code examples
javaandroidjsonlibgdxwriter

Writing on json file with libgdx


I'm new on this forum and I make an android app using libgdx and I want to save the player score on a json file. I can read the json file to get back the score but if the player do a better score I don't know how to write the new best score on the json file. I searched on many websites and I didn't find something good...

My json file is like that :

{ "scores":[
  {
    "level":1,
    "bestScore":100
  },
  {
    "level":2,
    "bestScore":100
  },
  {
    "level":3,
    "bestScore":100
  },
  {
    "level":4,
    "bestScore":100
  },
  {
    "level":5,
    "bestScore":100
  },
  {
    "level":6,
    "bestScore":100
  },
  {
    "level":7,
    "bestScore":100
  },
  {
    "level":8,
    "bestScore":100
  },
  {
    "level":9,
    "bestScore":100
  },
  {
    "level":10,
    "bestScore":100
  },
  {
    "level":11,
    "bestScore":100
  },
  {
    "level":12,
    "bestScore":100
  },
  {
    "level":13,
    "bestScore":100
  },
  {
    "level":14,
    "bestScore":100
  },
  {
    "level":15,
    "bestScore":100
  },
  {
    "level":16,
    "bestScore":100
  },
  {
    "level":17,
    "bestScore":100
  },
  {
    "level":18,
    "bestScore":100
  },
  {
    "level":19,
    "bestScore":100
  },
  {
    "level":20,
    "bestScore":100
  }
  ]
}

And for example, if the player do a score of 30 in the level 1, I want to replace the "100" from the level 1 by "30".

I hope somebody will can help me ! Thank you, I'm waiting for your answers !


Solution

  • Well, I may suggest you slightly different approach for that. I assume that you use Libgdx Json Api. First of all we need Java representation of json, something like:

    public class JsonResult {
        private ObjectMap<String, Integer> scores;
    
        public ObjectMap<String, Integer> getScores() {
            return scores;
        }
    
        public void setScores(ObjectMap<String, Integer> scores) {
            this.scores = scores;
        }
    }
    

    Here in the scores map the key is a level_ prefix with actual level number (e.g. level_1) and the value is a score for that level.

    This way your json structure would be similar to:

    {  
       "scores":{  
          "level_1":100,
          "level_2":100,
          "level_3":200,
          "level_4":300
           ....
    }
    

    So, then when you save new score:

    public void saveResult(Integer level, Integer newScore) {
        JsonResult jsonResult = json.fromJson(JsonResult.class, resultJson);
        ObjectMap<String, Integer> scores = jsonResult.getScores();
        String levelKey = "level_" + level;
        Integer scoreForLevel = scores.get(levelKey);
        if (newScore > scoreForLevel) {
             scores.put(levelKey, newScore);
             jsonResult.setScores(scores);
             json.toJson(jsonResult, resultJson);
        }
    }
    

    The logic is quite simple:

    1. Parse JsonResult object from file
    2. Get the score for the level from that object
    3. Compare with the new score and rewrite if needed.

    To create new json file:

    public void resetResultFile() {
        JsonResult jsonResult = new JsonResult();
        ObjectMap<String, Integer> initialScores = new ObjectMap<>(1);
        initialScores.put("level_1", 0);
        jsonResult.setScores(initialScores);
        json.toJson(jsonResult, resultJson);
    }