class javax.swing.JFrame声明了多个名为state的JSON字段

时间:2018-05-15 19:24:29

标签: java json gson

我正在尝试制作2D平铺游戏,我试图通过从JSON文件导入项目来添加项目。 我尝试使用GSON库导入JSON文件,但每当我运行代码时,我都会收到以下错误:

java.lang.IllegalArgumentException: class javax.swing.JFrame declares multiple JSON fields named state
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:172)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
    at com.google.gson.Gson.getAdapter(Gson.java:457)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
    at com.google.gson.Gson.getAdapter(Gson.java:457)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
    at com.google.gson.Gson.getAdapter(Gson.java:457)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
    at com.google.gson.Gson.getAdapter(Gson.java:457)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
    at com.google.gson.Gson.getAdapter(Gson.java:457)
    at com.google.gson.internal.bind.ArrayTypeAdapter$1.create(ArrayTypeAdapter.java:48)
    at com.google.gson.Gson.getAdapter(Gson.java:457)
    at com.google.gson.Gson.fromJson(Gson.java:921)
    at com.google.gson.Gson.fromJson(Gson.java:860)
    at dev.bako.tilegame.utils.JSONImporter.ItemJSONReader(JSONImporter.java:13)
    at dev.bako.tilegame.Game.init(Game.java:63)
    at dev.bako.tilegame.Game.run(Game.java:101)
    at java.lang.Thread.run(Thread.java:745)

JSONImporter类如下:

public class JSONImporter {

    public static void ItemJSONReader() throws Exception {

        Item[] items = new Gson().fromJson(new FileReader("res/JSON/Item.json"), Item[].class);//This is where I get the error
        System.out.println("Loaded file!" + items); 

    }

}

我试图导入的JSON文件:

{
  "Wood": {
    "id": 0
  },
  "Rock": {
    "id": 2
  }
}

1 个答案:

答案 0 :(得分:0)

您的JSON文件的结构表明您应该期待Map<String, Item>而不是Item[]

  • JSON数组被[]
  • 包围
  • 数组(列表)没有键,这里键明显可见,例如"Wood"

所以改变它应该引导你找到解决方案。

尽管如此,Swing和JFrame是跟踪堆栈的一部分,这表明它们是Item类的一部分。记住&#34;清洁代码&#34;或SOLID告诉(在这种情况下)一个类应该只有一个责任:传输数据通常由虚拟DTO执行,虚拟DTO应该没有与视图相关的逻辑。