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



我正在尝试制作一个 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
}
}

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

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

因此,改变这一点应该会引导您找到解决方案。

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

相关内容

最新更新