如何在JavaFX中读取JSON文件并将其显示到TableView



我正在为这个概念而挣扎。我有一个本地JSON文件,名为customers.JSON,其中有1000个客户,其中包含一堆关于他们的信息。

"Customers": [{
"id": 1,
"gender": "female",
"firstName": "Rose",
"lastName": "Ruffner",
"streetAddress": "3846 St. Paul Street",
"city": "St Catharines",
"province": "ON",
"postalCode": "L2S 3A1",
"emailAddress": "RoseMRuffner@pookmail.com",
"ccType": "Visa",
"bloodType": "B+",
"phoneNumber": "905-401-7824",
"pounds": 226.6,
"heightInCM": 156,
"purchases":
[
{
"id": 78,
"sku": "woo-vneck-tee-blue",
"name": "V-Neck T-Shirt - Blue",
"salePrice": 14.0,
"regularPrice": 15.0,
"image": "https://woocommercecore.mystagingwebsite.com/wp-content/uploads/2017/12/vnech-tee-blue-1.jpg"
},

现在,我需要使用JavaFX访问id、firstName、lastName、phoneNumber和他们在TableView中的总购买量。我最大的困难是找到从JSON文件中读取数据的最简单方法!

如果有人能帮助解释我如何读取customer.json文件并将数据填充到TableView中的id、firstName、lastName和sumOfTotalPurchases列中,我将不胜感激。

我有一个名为APIManager的类,尽管从技术上讲这不是一个API,这是我需要读取JSON数据的地方。然后我必须在TableViewController的Initialize方法中使用这些数据。

如果可以的话,请给我最简单的方法。

下面是我写的所有代码:

我的客户类别:

public class Customer {
@SerializedName("id")
private String m_customerId;
@SerializedName("firstName")
private String m_firstName;
@SerializedName("lastName")
private String m_lastName;
@SerializedName("phoneNumber")
private String m_phoneNumber;
@SerializedName("purchases")
private Product[] m_product;
public Customer(){}
public Customer(String customerId, String firstName, String lastName, String phoneNumber, Product[] product)
{
this.m_customerId = customerId;
this.m_firstName = firstName;
this.m_lastName = lastName;
this.m_phoneNumber = phoneNumber;
this.m_product = product;
}
public String getCustomerId() {
return m_customerId;
}
public String getFirstName() {
return m_firstName;
}
public String getLastName() {
return m_lastName;
}
public String getPhoneNumber() {
return m_phoneNumber;
}
public Product[] getProduct() {
return m_product;
}

我的产品类别:

public class Product {
@SerializedName("sku")
private String m_sku;
@SerializedName("name")
private String m_name;
@SerializedName("salePrice")
private double m_salePrice;
@SerializedName("regularPrice")
private double m_regularPrice;
@SerializedName("image")
private String m_imageURL;
public Product(){}
public Product(String sku, String name, double salePrice, double regularPrice, String imageURL){
this.m_sku = sku;
this.m_name = name;
this.m_salePrice = salePrice;
this.m_regularPrice = regularPrice;
this.m_imageURL = imageURL;
}
public String getSku() {
return m_sku;
}
public String getName() {
return m_name;
}
public double getSalePrice() {
return m_salePrice;
}
public double getRegularPrice() {
return m_regularPrice;
}
public String getImageURL() {
return m_imageURL;
}
@Override
public String toString(){
return m_name +"- $" + m_salePrice;
}

我的APIManager类:

public class APIManager
{
/********************* SINGLETON SECTION *****************************/
// Step 1. private static instance member variable
private static APIManager m_instance = null;
// Step 2. make the default constructor private
private APIManager() {}
// Step 3. create a public static entry point / instance method
public static APIManager Instance()
{
// Step 4. Check if the private instance member variable is null
if(m_instance == null)
{
// Step 5. Instantiate a new APIManager instance
m_instance = new APIManager();
}
return m_instance;
}
/*********************************************************************/
/* TODO -- Fill in with useful methods to read Customer information */
}

我的TableViewController类:

public class TableViewController implements Initializable {
@FXML
private Label saleLabel;
@FXML
private Label msrpLabel;
@FXML
private Label savingsLabel;
@FXML
private Label rowsInTableLabel;
@FXML
private TableView<Customer> tableView;
@FXML
private TableColumn<Customer, Integer> idColumn;
@FXML
private TableColumn<Customer, String> firstNameColumn;
@FXML
private TableColumn<Customer, String> lastNameColumn;
@FXML
private TableColumn<Customer, String> phoneColumn;
@FXML
private TableColumn<Customer, String> totalPurchaseColumn;
@FXML
private ListView<Product> purchaseListView;
@FXML
private ImageView imageView;
@FXML
private void top10Customers()
{
System.out.println("called method top10Customers()");
}
@FXML
private void customersSavedOver5()
{
System.out.println("called method customersSavedOver5()");
}
@FXML
private void loadAllCustomers()
{
System.out.println("called method loadAllCustomers");
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
}
}

您可以使用gson来解析json。

以下是关于如何读取/加载customer.json文件的数据的示例:

public void saveCustomers(Customer[] customers, String filename) {
try(FileWriter writer = new FileWriter(filename)) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
gson.toJson(customers, writer);

} catch (JsonIOException | IOException e) {
e.printStackTrace();
// handle exception
}
}
public Customer[] loadCustomers(String filename) {
Customer[] customers;
try (FileReader reader = new FileReader(filename)) {
Gson gson = new GsonBuilder().create();

customers = gson.fromJson(reader, Customer[].class);

} catch (NullPointerException | IOException | JsonIOException | JsonSyntaxException e) {
e.printStackTrace();
// handle exception
}
return customers;
}

相关内容

  • 没有找到相关文章

最新更新