使用来自于Json()

时间:2018-05-22 23:23:38

标签: java json javafx gson

尝试使用GSON解析JSON文件时出现此异常:

Exception in thread "JavaFX Application Thread"   java.lang.IllegalArgumentException: class com.sun.javafx.jmx.HighlightRegion declares multiple JSON fields named hash

我已经测试了一些导致这种情况的原因,我发现只有当我创建一个将Event对象作为成员的Object时才会发生这种情况。在这种情况下,我正在尝试创建最终具有Event对象的List Quest对象。

package Quests;

import Events.EventSequence;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Quest {

    public static void parseQuest() {

        Gson gson = new Gson();
        FileReader questGSON;
        try {
            questGSON = new FileReader(QUEST_FILE);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return;
        }
        JsonObject questJSON = gson.fromJson(questGSON, JsonObject.class);

        questList = new ArrayList<>();

        for(Map.Entry<String, JsonElement> element : questJSON.entrySet()) {
            questList.add(gson.fromJson(element.getValue(), Quest.class));
        }
    }

}

我通过以下方式在该类中使用javafx:

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;

public abstract class Event implements EventHandler<ActionEvent> {

    // members of this class
    private String title, text;
    private Button[] buttonSet;
    protected Entity other;
    protected Event nextEvent;

    public abstract Event chooseNewEvent(String command);

    @Override
    public void handle(ActionEvent event) {
        // Get the name of the button
        String command = ((Button) event.getSource()).getText();

        displayNewEvent(chooseNewEvent(command));
    }
}

我能找到的唯一信息是: https://github.com/wpilibsuite/shuffleboard/issues/358

有人能指点我解决我的错误吗?

1 个答案:

答案 0 :(得分:0)

我发现它是javafx.scene.control.ButtonEvent中的一个Button数组导致了问题。我通过使它transient来修复它,所以它会被GSON忽略而不会引起任何问题。

然而,我不知道为什么Button数组会导致这个异常。有什么想法吗?

相关问题