gpt4 book ai didi

json - 读取任意json数据到javafx treeview,只显示其中任意数组的第一个元素

转载 作者:行者123 更新时间:2023-12-04 16:44:39 24 4
gpt4 key购买 nike

我需要在 javafx TreeView 上显示一个 json 文件,json 的结构是未知的。如网站:json viewer site我显示树供用户选择值的路径(如 xml 的 xpath),因此如果 json 太大,我只需要显示 json 中任何数组的第一个元素。

例如原始数据为:

{
name:"tom",
schools:[
{
name:"school1",
tags:["maths","english"]
},
{
name:"school2",
tags:["english","biological"]
},
]
}

我想展示: enter image description here

再强调一遍:json的结构未知,只是一个例子。

最佳答案

除了递归处理 json 并根据元素信息创建 TreeItem 结构之外,别无选择。

(添加符号可能有更好的方法,但我没有找到合适的图标。)

private static final String INPUT = "{\n"
+ " name:\"tom\",\n"
+ " schools:[\n"
+ " {\n"
+ " name:\"school1\",\n"
+ " tags:[\"maths\",\"english\"]\n"
+ " },\n"
+ " {\n"
+ " name:\"school2\",\n"
+ " tags:[\"english\",\"biological\"]\n"
+ " },\n"
+ " ]\n"
+ "}";

private static final Image JSON_IMAGE = new Image("/image/1slrh.png");

private static void prependString(TreeItem<Value> item, String string) {
String val = item.getValue().text;
item.getValue().text = (val == null
? string
: string + " : " + val);
}

private enum Type {
OBJECT(new Rectangle2D(45, 52, 16, 18)),
ARRAY(new Rectangle2D(61, 88, 16, 18)),
PROPERTY(new Rectangle2D(31, 13, 16, 18));

private final Rectangle2D viewport;

private Type(Rectangle2D viewport) {
this.viewport = viewport;
}

}

private static final class Value {

private String text;
private final Type type;

public Value(Type type) {
this.type = type;
}

public Value(String text, Type type) {
this.text = text;
this.type = type;
}

}

private static TreeItem<Value> createTree(JsonElement element) {
if (element.isJsonNull()) {
return new TreeItem<>(new Value("null", Type.PROPERTY));
} else if (element.isJsonPrimitive()) {
JsonPrimitive primitive = element.getAsJsonPrimitive();
return new TreeItem<>(new Value(primitive.isString()
? '"' + primitive.getAsString() + '"'
: primitive.getAsString(), Type.PROPERTY));
} else if (element.isJsonArray()) {
JsonArray array = element.getAsJsonArray();
TreeItem<Value> item = new TreeItem<>(new Value(Type.ARRAY));
// for (int i = 0, max = Math.min(1, array.size()); i < max; i++) {
for (int i = 0, max = array.size(); i < max; i++) {
TreeItem<Value> child = createTree(array.get(i));
prependString(child, Integer.toString(i));
item.getChildren().add(child);
}
return item;
} else {
JsonObject object = element.getAsJsonObject();
TreeItem<Value> item = new TreeItem<>(new Value(Type.OBJECT));
for (Map.Entry<String, JsonElement> property : object.entrySet()) {
TreeItem<Value> child = createTree(property.getValue());
prependString(child, property.getKey());
item.getChildren().add(child);
}
return item;
}
}

@Override
public void start(Stage primaryStage) {
JsonParser parser = new JsonParser();
JsonElement root = parser.parse(INPUT);

TreeItem<Value> treeRoot = createTree(root);
TreeView<Value> treeView = new TreeView<>(treeRoot);
treeView.setCellFactory(tv -> new TreeCell<Value>() {
private final ImageView imageView;

{
imageView = new ImageView(JSON_IMAGE);
imageView.setFitHeight(18);
imageView.setFitWidth(16);
imageView.setPreserveRatio(true);
setGraphic(imageView);
}

@Override
protected void updateItem(Value item, boolean empty) {
super.updateItem(item, empty);

if (empty || item == null) {
setText("");
imageView.setVisible(false);
} else {
setText(item.text);
imageView.setVisible(true);
imageView.setViewport(item.type.viewport);
}
}


});

final Scene scene = new Scene(treeView);

primaryStage.setScene(scene);
primaryStage.show();
}

关于json - 读取任意json数据到javafx treeview,只显示其中任意数组的第一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51039132/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com