在TableView中显示JSON [英] Show JSON in TableView

查看:44
本文介绍了在TableView中显示JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

表单JSON:
"[{"key1": 1, "key2": 2}, {"key1": 3, "key2": 4}]"

它需要如下所示:

key1 key2
1 2
3 4

有什么建议吗?

推荐答案

这可以分为两部分。

  1. 使用GSONJSON Array解析为ArrayPOJOs
  2. TableView中显示List,共Objets

键代码

//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);

main

import com.google.gson.Gson;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
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(new PropertyValueFactory<>("keyOne"));


        TableColumn<Data, String> column2 = new TableColumn<>("Key Two");
        column2.setCellValueFactory(new PropertyValueFactory<>("keyTwo"));


        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();
    }    
}

module-info.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。

输出

这篇关于在TableView中显示JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆