本文介紹了使用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ì)大家有所幫助,