Search code examples
javajsonjavafxuiviewcontrollerscenebuilder

How can I read a JSON file and display it to a TableView in JavaFX?


I am struggling with this concept. I have a local JSON file, named customers.json, with 1000 customers containing a bunch of information about them.

"Customers": [{
    "id": 1,
    "gender": "female",
    "firstName": "Rose",
    "lastName": "Ruffner",
    "streetAddress": "3846 St. Paul Street",
    "city": "St Catharines",
    "province": "ON",
    "postalCode": "L2S 3A1",
    "emailAddress": "[email protected]",
    "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"
    },

Now, I need to access the id, firstName, lastName, phoneNumber and sum of their total purchases put into a TableView using JavaFX. My biggest struggle is finding the simplest way to read the data from the JSON file!

If someone could help explain how I can read the customers.json file and populate the data into the id, firstName, lastName and sumOfTotalPurchases columns in a TableView, that would be greatly appreciated.

I have a class made called APIManager, even though this is not technically an API, which is where I need to read the JSON data. Then I have to use this data in the Initialize method of my TableViewController.

If you could, please give me the simplest way to do this.

Here is all the code I have written below:

My Customer Class:

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

My Products Class:

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

My APIManager Class:

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 */

}

And my TableViewController Class:

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) {

    }
}

Solution

  • You could use gson to parse the json.

    Here's an example on how to read/load the data of your customer.json file:

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