使用JavaFX将JSONObject/字符串添加到树视图中 [英] Add a JSONObject/String to a TreeView using JavaFx

查看:0
本文介绍了使用JavaFX将JSONObject/字符串添加到树视图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JavaFX和SceneBuilder在树视图中显示JSON文件。 我按照本教程https://www.geeksforgeeks.org/parse-json-java/阅读了JSON文件,但在解析它时遇到了问题。

我尝试使用JSON Simple库解析JSON文件(我使用界面中的按钮上传的文件),并将JSONObts转换为字符串,但我不知道如何在TreeView中添加这些字符串。首先,我尝试将字符串转换为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); 
}

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

这是一个额外的问题,我不需要答案:有没有更简单的方法来编写代码?你有什么推荐的?

推荐答案

此方法将使用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));

这篇关于使用JavaFX将JSONObject/字符串添加到树视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆