在 TableView 中显示 JSON

Arg*_*gun 2 generics json javafx tableview

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

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

它需要看起来像这样:

键1 键2
1 2
3 4

有什么建议吗?

Sed*_*ick 5

这可以分为两部分。

  1. 用于GSON将 a 解析JSON ArrayArrayof POJOs
  2. 在 a 中List显示a 。ObjetsTableView

关键代码

//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);
Run Code Online (Sandbox Code Playgroud)

主要的

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();
    }
} 
Run Code Online (Sandbox Code Playgroud)

数据类

/**
 *
 * @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();
    }    
}
Run Code Online (Sandbox Code Playgroud)

模块信息.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;
    
}
Run Code Online (Sandbox Code Playgroud)

使用GSON版本2.8.9。

输出

在此输入图像描述

  • 看起来对于所有这些 _TableView 中的 JSON 问题来说都是一个很好的答案:) (2认同)
  • 仅供参考:在常见问题解答中添加了对此 QA 的引用 (2认同)