【发布时间】:2018-05-15 19:24:29
【问题描述】:
我正在尝试制作 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
}
}
【问题讨论】:
-
Item的结构是什么,当您的数据似乎是Map<String, Item>时,为什么要反序列化Item[]?那么 Swing 和 JFrame 与这一切,数据反序列化的过程有什么关系呢?并且请不要告诉您没有要反序列化的 DTO,该 DTO 与视图层无关。 -
我将项目的类型更改为 Map
现在它可以工作了,我没有想到使用 Map,所以我专注于 ArrayList,我认为这就是所有问题是。我还修改了方法的实现,因为我错误地放入了视图层,现在我没有新的问题或错误。 -
因为它帮助我把我的评论作为答案。