gpt4 book ai didi

json - 在 TableView 中显示 JSON

转载 作者:行者123 更新时间:2023-12-02 01:51:24 24 4
gpt4 key购买 nike

我正在使用 JavaFX 开发 JSON 数组的通用编辑器。
表中的显示方式是列作为键,行中的值更具描述性。一个 JSONObject 中可以有不同数量的键。

JSON 格式:
"[{\"key1\": 1, \"key2\": 2}, {\"key1\": 3, \"key2\": 4}]"

它需要看起来像这样:

<表类=“s-表”><标题>键1键2 <正文>1234

有什么建议吗?

最佳答案

这可以分为两部分。

  1. 使用 GSONJSON 数组 解析为 POJOArray
  2. TableView 中显示对象列表

关键代码

//Add data to the TableView!
String jsonString = "[{\"keyOne\":\"1\", \"keyTwo\":\"2\"}, {\"keyOne\":\"3\", \"keyTwo\":\"4\"}]";
Gson gson = new Gson();
Data[] dataList = gson.fromJson(jsonString, Data[].class);
ObservableList<Data> observableList = FXCollections.observableArrayList(dataList);
tableView.setItems(observableList);

主要

import com.google.gson.Gson;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.stage.Stage;
import javafx.scene.layout.StackPane;


public class App extends Application {
public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage stage){
TableView<Data> tableView = new TableView();

TableColumn<Data, String> column1 = new TableColumn<>("Key One");
column1.setCellValueFactory((cdf) -> new SimpleStringProperty(cdf.getValue().getKeyOne()));


TableColumn<Data, String> column2 = new TableColumn<>("Key Two");
column2.setCellValueFactory((cdf) -> new SimpleStringProperty(cdf.getValue().getKeyTwo()));


tableView.getColumns().add(column1);
tableView.getColumns().add(column2);

//Add data to the TableView!
String jsonString = "[{\"keyOne\":\"1\", \"keyTwo\":\"2\"}, {\"keyOne\":\"3\", \"keyTwo\":\"4\"}]";
Gson gson = new Gson();
Data[] dataList = gson.fromJson(jsonString, Data[].class);
ObservableList<Data> observableList = FXCollections.observableArrayList(dataList);
tableView.setItems(observableList);

Scene scene = new Scene(new StackPane(tableView));

stage.setTitle("JavaFX 13");
stage.setScene(scene);
stage.show();
}
}

数据类

/**
*
* @author sedj601
*/
public class Data {
private String keyOne;
private String keyTwo;


public Data(String keyOne, String keyTwo) {
this.keyOne = keyOne;
this.keyTwo = keyTwo;
}

public String getKeyOne() {
return keyOne;
}

public void setKeyOne(String keyOne) {
this.keyOne = keyOne;
}

public String getKeyTwo() {
return keyTwo;
}

public void setKeyTwo(String keyTwo) {
this.keyTwo = keyTwo;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Data{keyOne=").append(keyOne);
sb.append(", keyTwo=").append(keyTwo);
sb.append('}');
return sb.toString();
}
}

模块信息.java

module com.mycompany.javafx_test_2 {
requires javafx.controls;
exports com.mycompany.javafx_test_2;

opens com.mycompany.javafx_test_2 to com.google.gson;
requires com.google.gson;

}

使用GSON版本2.8.9。

输出

enter image description here

关于json - 在 TableView 中显示 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70302119/

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