這可能是一個新手問題。我無法用我的解析成以.json一個HashMap我可以輕鬆地編寫JSON文件這是我怎麼知道它是正確寫入,但加載它在即時得到一個錯誤:解析HashMap Json .fromJson
Caused by: java.lang.InstantiationException: java.lang.Class<com.johnny.gamerpg.inventoryController$Weapon> has no zero argument constructor
Caused by: com.badlogic.gdx.utils.SerializationException: Class cannot be created (non-static member class): com.johnny.gamerpg.inventoryController$Weapon
Caused by: com.badlogic.gdx.utils.reflect.ReflectionException: Could not instantiate instance of class: com.johnny.gamerpg.inventoryController$Weapon
我相信我有一個零構造函數和那些類正確設置,因爲我正在使用它們來編寫.json。
以下是這些類。
public class item {
public String name;
public item() {}
}
public class Weapon extends item {
int damageMin = 9;
int damageMax = 15;
int priceBuy = 1;
int priceSell = 2;
public Weapon() {}
}
public class Sword extends Weapon{
public Sword() {}
}
public class Dagger extends Weapon{
public Dagger() {}
}
private FileHandle file = Gdx.files.local("items.json");
private HashMap<String, item> items = new HashMap<String, item>();
private void load(){
Json json = new Json();
items = json.fromJson(HashMap.class, file);
}
我已經四處搜索,還沒有找到任何完全一樣的東西。所以這可能意味着我在做一些愚蠢的事情!無論如何,這裏是閱讀Json文件,並感謝任何建議將是驚人的。
{
"SwordOfDeath": {
"class": "com.johnny.gamerpg.inventoryController$Sword",
"name": "Sword of Death",
"damageMax": 15,
"damageMin": 2,
"priceBuy": 1,
"priceSell": 2
},
"DaggerOfDeath": {
"class": "com.johnny.gamerpg.inventoryController$Dagger",
"name": "Dagger of Death",
"damageMax": 15,
"damageMin": 2,
"priceBuy": 1,
"priceSell": 2
}
}
謝謝!
編輯: 進口, package com.johnny.gamerpg;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.Json;
import com.badlogic.gdx.utils.JsonWriter;
import java.util.HashMap;
你能顯示你的進口報表嗎? – John
@John **更新!** – johnnyboy5566
而且你的java類都在同一個文件或單獨的文件? – John