Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用GSON';s fromJson()_Java_Json_Javafx_Gson - Fatal编程技术网

Java 使用GSON';s fromJson()

Java 使用GSON';s fromJson(),java,json,javafx,gson,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 我已经测试了一些导致这种情况的原因,我发现只有当我创建了一个包含事件对象作为成员的对象时才会发生这种情况。在本

我在尝试使用GSON解析JSON文件时遇到此异常:

Exception in thread "JavaFX Application Thread"   java.lang.IllegalArgumentException: class com.sun.javafx.jmx.HighlightRegion declares multiple JSON fields named hash
我已经测试了一些导致这种情况的原因,我发现只有当我创建了一个包含事件对象作为成员的对象时才会发生这种情况。在本例中,我试图创建一个任务对象的
列表
,这些任务对象最终会有事件对象

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));
        }
    }

}
包任务;
导入Events.EventSequence;
导入com.google.gson.gson;
导入com.google.gson.JsonElement;
导入com.google.gson.JsonObject;
导入java.io.FileNotFoundException;
导入java.io.FileReader;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.Map;
公开课探索{
公共静态void parseQuest(){
Gson Gson=新的Gson();
文件阅读器questGSON;
试一试{
questGSON=新文件读取器(QUEST_文件);
}catch(filenotfounde异常){
e、 printStackTrace();
返回;
}
JsonObject questJSON=gson.fromJson(questGSON,JsonObject.class);
questList=newarraylist();
for(Map.Entry元素:questJSON.entrySet()){
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));
    }
}
导入javafx.event.ActionEvent;
导入javafx.event.EventHandler;
导入javafx.scene.control.Button;
公共抽象类事件实现EventHandler


有人能给我指出一些方向来修复我的bug吗?

我发现是我的
事件
类中的
javafx.scene.control.Button
中的一个按钮数组导致了这个问题。我修复了它,使它成为瞬态的,这样它就会被GSON忽略,而不会引起任何问题


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

为什么要将内部
com.sun
类封送至JSON或从JSON封送至?住手!!那是什么?我怎么才能停止那样做?你在做什么?如果您试图将某种javafxui组件转换为JSON,那么似乎唯一可能发生这种情况的方法就是。您应该只对数据使用JSON,而不是对任何类型的UI组件使用JSON。我正在尝试将添加的JSON文件转换为对象。您的
Quest
类是什么样子的,如果父类和子类具有相同名称的字段,则它必须具有
com.sun.javafx.jmx.HighlightRegion
字段