paw_rd:
私はJavaFXのとSceneBuilderを使用してツリービューでJSONファイルを表示しようとしています。私は、このチュートリアルの後https://www.geeksforgeeks.org/parse-json-java/を JSONファイルを読み取るため、私は問題、それを解析しています。
私が使用して(私はインタフェースのボタンを使用してアップロードするファイル)JSONファイルを解析しようとしたJSONの簡単なライブラリをし、文字列にJSONObjectsをキャストが、私は、ツリービューでこれらの文字列を追加する方法がわかりません。まず、私はツリー項目に文字列をキャストしようとしたが、それがすべてでは動作しません。
私は、解析しようとしている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);
}
長い話を短く:AにJSONObject /文字列を追加する方法 TreeView using JavaFx and JSON_simple library?
これは私が必要な必要な答えをしません、追加の質問です:それはそこにコードを書くための任意の簡単な方法は?おすすめは何ですか?
ミズ :
このメソッドは、再帰的に構築されます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));