I've been messing around with a JSON file save system, and every time I save it, the keys end up in a different order than what I put them as in the JSON file. The program still runs fine regardless of order, but as the list of items in the file gets bigger, it would be easier to edit small things in the file if I knew exactly where they are. Here's my code:
public static void CreateJSON() throws JSONException {
// List<String> projList = UpgradeJSON.GetProjList();
// List<String> statsList = UpgradeJSON.GetStatsList();
List<String> projList = new ArrayList<>();
List<String> statsList = new ArrayList<>();
projList.add("Reave Bomb");
projList.add("Purple Fire");
projList.add("Twilight Ball");
statsList.add("speed");
statsList.add("damage");
JSONObject selfJSON = new JSONObject();
JSONObject upgradeJSON = new JSONObject();
for (String i : projList) {
JSONObject projObj = new JSONObject();
for (String j : statsList) {
projObj.put(j.toLowerCase(), 1);
}
upgradeJSON.put(i, projObj);
}
selfJSON.put("name", "");
selfJSON.put("credits", 500);
selfJSON.put("health", 100);
selfJSON.put("aura", 100);
selfJSON.put("upgrades", upgradeJSON);
System.out.println(selfJSON.toString());
try (FileWriter file = new FileWriter("val/player.json")) {
file.write(selfJSON.toString(4));
} catch (IOException e) {
e.printStackTrace();
}
}
The JSON file it creates looks like this:
{
"credits": 2910,
"name": "",
"health": 100,
"upgrades": {
"Reave Bomb": {
"damage": 1,
"speed": 1
},
"Twilight Ball": {
"damage": 1,
"speed": 1
},
"Purple Fire": {
"damage": 1,
"speed": 1
}
},
"aura": 100
}
Also, the library I'm using is org.json.