java - 使用 JavaFx 将 JSONObject/String 添加到 TreeView

标签 java json javafx treeview treeviewitem

我正在尝试使用 JavaFx 和 SceneBuilder 在 TreeView 中显示 JSON 文件。 我遵循了本教程 https://www.geeksforgeeks.org/parse-json-java/用于读取 JSON 文件,但我在解析它时遇到问题。

我尝试使用 JSON simple 库解析 JSON 文件(我使用界面中的按钮上传的文件)并将 JSONObjects 转换为字符串,但我不知道如何添加TreeView 中的那些字符串。首先,我尝试将 String 转换为 TreeItem ,但它根本不起作用。

我要说的是,我尝试解析的 JSON 文件具有复杂的结构,而且我发现以与现在相同的方式解析它有点困难。

我的 JSON 文件的简化结构:

{
    "root": {
      "array": [
        {
          "element1": "text",
          "element2": {
            "detail1Element2": "text",
            "detail2Element2": "text"
          },
          "element3": {
            "detail1Element3": "text",
            "detail2Element3": "text"
          },

          "element4": {
            "subElement4-1": {
              "arraySubElement4-1": [
                {
                  "detail1SubSubElement4-1": "text",
                  "detail2SubSUbElement4-1": "text"
                },
                {
                  "detail1SubSubElement4-1": "text",
                  "detail2SubSubElement4-1": "text"
                }
              ]
            },
            "subElement4-2": {
              "arraySubElement4-2": [
                {
                  "detail1SubSubElement4-2": "text",
                  "detail2SubSubElement4-2": "text",
                  "detail3SubSubElement4-2": "text",
                  "detail3SubSubElement4-2": "text"
                },
                 {
                  "detail1SubSubElement4-2": "text",
                  "detail2SubSubElement4-2": "text",
                  "detail3SubSubElement4-2": "text",
                  "detail3SubSubElement4-2": "text"
                }
              ]
            },
            "element5": "text",
            "element6": "text",
            "element7": "text"
          }
        },
        {
         //second array element; it has the same structure as the first one
        },
        {
         //another array element; it has the same structure as the first one
        }

      ]
    }
}

我开始写的解析JSON的方法:

@FXML
void parsingJSON(ActionEvent event) throws FileNotFoundException, IOException, ParseException {

    Object obj = new JSONParser().parse(new FileReader(fileJSON));
    JSONObject jo = (JSONObject) obj;

    JSONObject root = (JSONObject) jo.get("root");
    JSONArray array = (JSONArray) root.get("array");
    JSONObject arrayElement = null;

    Iterator i = array.iterator();
    TreeItem<String> rootTreeItem = new TreeItem<String>("Root");
    TreeItem<String>[] element1Value = null;
    String[] element1ValueS = null;

    int iterator = 0;
    while (i.hasNext()) {
        arrayElement = (JSONObject) i.next();
        element1ValueS[iterator] = (String) arrayElement.get("text");
        System.out.println(element1ValueS);
        iterator++;
    }

    for (int i1 = 0; i1 < element1ValueS.length; i1++) {
        rootTreeItem.getChildren().add((TreeItem<String>) element1ValueS[i]); // here's an error
    }

    TreeItem<String> dataText = new TreeItem<String>("TEXT");

    treeviewJSON.setRoot(rootTreeItem); 
}

长话短说:如何使用 JavaFx 和 JSON_simple 库将 JSONObject/String 添加到 TreeView?

这是一个附加问题,我不需要回答:有没有更简单的方法来编写代码?你有什么建议?

最佳答案

此方法将使用 org.json.simple 元素递归构建 TreeItem:

@SuppressWarnings("unchecked")
private static TreeItem<String> parseJSON(String name, Object json) {
    TreeItem<String> item = new TreeItem<>();
    if (json instanceof JSONObject) {
        item.setValue(name);
        JSONObject object = (JSONObject) json;
        ((Set<Map.Entry>) object.entrySet()).forEach(entry -> {
            String childName = (String) entry.getKey();
            Object childJson = entry.getValue();
            TreeItem<String> child = parseJSON(childName, childJson);
            item.getChildren().add(child);
        });
    } else if (json instanceof JSONArray) {
        item.setValue(name);
        JSONArray array = (JSONArray) json;
        for (int i = 0; i < array.size(); i++) {
            String childName = String.valueOf(i);
            Object childJson = array.get(i);
            TreeItem<String> child = parseJSON(childName, childJson);
            item.getChildren().add(child);
        }
    } else {
        item.setValue(name + " : " + json);
    }
    return item;
}

解析文件:

JSONParser parser = new JSONParser();
JSONObject root = (JSONObject) parser.parse(new FileReader(new File("json_file_path")));

TreeView<String> treeView = new TreeView<>();
treeView.setRoot(parseJSON("root_object", root));

关于java - 使用 JavaFx 将 JSONObject/String 添加到 TreeView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60953087/

相关文章:

java - 从 jar 中复制 Eclipse p2 接触点

java - 从表单中获取信息并将其粘贴到jsp

java - AES/CBC 真的需要 IV 参数吗?

php - 想在 laravel 上返回值 json

java - 如何在进度条中显示进度百分比?

css - 如何从javafx中的一个样式表为具有相同类的节点设置不同的样式

java - 无法通过 Selenium 和 Java 在 https ://spicejet. com 中选择出发日期

json - 能够使用JSON对象创建Hive表,但并非所有对象都被捕获到各自的属性中

json - 使用Groovy脚本从JSON响应中一一打印特定节点的所有值

java - 如何让多个文本框以随机方向移动,并且它们之间有特定的时间间隔?