Ich versuche derzeit, eine Sammlung von JSONObjects in meiner .yml-Datei zu speichern. Aber ich bin an einem Punkt fest, wenn es die JSONObjects aus der Datei laden soll.Wie kann ich eine JSONObject-Sammlung in einer Datei speichern und laden?
Meine Quellen:
static boolean saveChest(JSONObject obj){
chestList.add(obj);
FC = YamlConfiguration.loadConfiguration(STORAGE);
FC.set("chests", chestList.toArray().toString());
try {
FC.save(STORAGE);
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
public static boolean loadChests(){
try{
chestList.clear();
if(!STORAGE.exists()){
STORAGE.getParentFile().mkdirs();
STORAGE.createNewFile();
}
FC = YamlConfiguration.loadConfiguration(STORAGE);
Collection<JSONObject> list = new ArrayList<JSONObject>();
list.addAll((Collection<? extends JSONObject>) (JSONObject) FC.get("chests"));
for(JSONObject temp : list){
chestList.add(temp);
Location loc = (Location) temp.get("loc");
Hologram holo = HologramsAPI.createHologram(PLUGIN, loc.add(0.5, 2, 0.5));
holo.appendTextLine("§e§lVote Chest");
holo.appendTextLine("§bRight-Click with Vote Key to open!");
}
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}
}
public static boolean registerChest(Block chest, ChestType type){
JSONObject temp = new JSONObject();
switch(type){
case VOTE:
temp.put("loc", chest.getLocation().toString());
temp.put("type", type.toString());
Hologram holo = HologramsAPI.createHologram(PLUGIN, chest.getLocation().add(0.5, 2, 0.5));
holo.appendTextLine("§e§lVote Chest");
holo.appendTextLine("§bRight-Click with Vote Key to open!");
break;
}
saveChest(temp);
return false;
}
Ich erhalte die folgende Fehlermeldung:
Chest.java:245:
list.addAll((Collection<? extends JSONObject>) (JSONObject) FC.get("chests"));
jetzt fast 2 Stunden Der Versuch, behebe das, aber ich nehme an, ich bekomme einfach nicht, was ich tun muss, um es zu reparieren. Können Sie mir vielleicht helfen?
Es scheint, dass Ihr JSON nicht gültig ist. Daher kann es nicht an JSONObject übergeben werden. Kannst du deine FC.get ("Truhen") bekommen und prüfen, ob sie gültig ist oder nicht ?! –
Falls Ihr JSON gültig ist, können Sie folgendes tun: JSONObject jsonObject = new JSONObject (FC.get ("chests")); –