日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長(zhǎng)提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請(qǐng)做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢(xún)客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

本文介紹了使用JavaFX將JSONObject/字符串添加到樹(shù)視圖中的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我正在嘗試使用JavaFX和SceneBuilder在樹(shù)視圖中顯示JSON文件。
我按照本教程https://www.geeksforgeeks.org/parse-json-java/閱讀了JSON文件,但在解析它時(shí)遇到了問(wèn)題。

我嘗試使用JSON Simple庫(kù)解析JSON文件(我使用界面中的按鈕上傳的文件),并將JSONObts轉(zhuǎn)換為字符串,但我不知道如何在TreeView中添加這些字符串。首先,我嘗試將字符串轉(zhuǎn)換為T(mén)reeItem,但根本不起作用。

我想說(shuō)我試圖解析的JSON文件有一個(gè)復(fù)雜的結(jié)構(gòu),我發(fā)現(xiàn)用我現(xiàn)在正在做的相同方式解析它有點(diǎn)困難。

我的JSON文件的簡(jiǎn)化結(jié)構(gòu):

{
    "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
        }

      ]
    }
}

我開(kāi)始編寫(xiě)的解析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); 
}

長(zhǎng)話短說(shuō):如何將JSONObject/字符串添加到TreeView using JavaFx and JSON_simple library?

這是一個(gè)額外的問(wèn)題,我不需要答案:有沒(méi)有更簡(jiǎn)單的方法來(lái)編寫(xiě)代碼?你有什么推薦的?

推薦答案

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

這篇關(guān)于使用JavaFX將JSONObject/字符串添加到樹(shù)視圖中的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,

分享到:
標(biāo)簽:JavaFX JSONObject 字符串 添加 視圖
用戶(hù)無(wú)頭像

網(wǎng)友整理

注冊(cè)時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊(cè)賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過(guò)答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫(kù),初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績(jī)?cè)u(píng)定2018-06-03

通用課目體育訓(xùn)練成績(jī)?cè)u(píng)定