2016-07-11 2 views
1

現在、JSONObjectのコレクションを.ymlファイルに保存しようとしています。 しかし、ファイルからJSONObjectをロードすると、ある時点で動かなくなってしまいます。ファイルにJSONObjectコレクションを保存してロードするにはどうすればよいですか?

私の情報源:

hastebin of my error

Chest.java:245:ほぼ2時間、今しようと

list.addAll((Collection<? extends JSONObject>) (JSONObject) FC.get("chests")); 

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; 
} 

私は、次のエラーを取得していますそれを修正するが、私はそれを修正するために何をしなければならないかを仮定している。 私を助けてもらえますか?

+0

JSONが有効ではないようです。したがって、JSONObjectにキャストすることはできません。 FC.get( "chests")を入手し、それが有効かどうかチェックできますか? –

+0

あなたのJSONが有効な場合は、次のようにすることができます:JSONObject jsonObject = new JSONObject(FC.get( "chests")); –

答えて

0

私はJSONObjectsのコレクションに(YamlConfigurationオブジェクトから返された)Stringのコレクションを直接キャストしようとしていると思います。該当するStringがJSON形式であれば、新しいJSONParserをインスタンス化し、各StringをJSONObjectに解析できます。例えば:

Collection<JSONObject> list = new ArrayList<JSONObject>(); 
JSONParser jsonParser = new JSONParser(); 
for (String s : FC.get("chests")) { 
    list.add((JSONObject) jsonParser.parse(s)); 
} 

か、のaddAll()を使用するためには、あなたの代わりに最初の文字列のコレクションを作成することができ、およびコレクションをループしながらJSONObjectsにそれらを解析します。

関連する問題