【问题标题】:class javax.swing.JFrame declares multiple JSON fields named statejavax.swing.JFrame 类声明了多个名为 state 的 JSON 字段
【发布时间】: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,我认为这就是所有问题是。我还修改了方法的实现,因为我错误地放入了视图层,现在我没有新的问题或错误。
  • 因为它帮助我把我的评论作为答案。

标签: java json gson


【解决方案1】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 2021-02-13
    • 2017-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-04
    • 2016-01-25
    • 2014-09-09
    相关资源
    最近更新 更多