使用JavaFx将JSONObject /字符串添加到TreeView

问题描述 投票:2回答:1

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

我尝试使用JSON简单库解析JSON文件(我使用界面中的按钮上传的文件),并将JSONObjects转换为字符串,但我不知道如何在树视图。首先,我尝试将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); 
}

长话短说:如何向TreeView using JavaFx and JSON_simple library?添加JSONObject /字符串

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

java json javafx treeview treeviewitem
1个回答
0
投票

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

@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));
© www.soinside.com 2019 - 2024. All rights reserved.