0

Hello I`m working at a javafx application and i want to populate a tableview or a listview with my json data: { "username" : "Viviana", "password" : "{��O��s�\u000F�P�g�5\u000B��\u001E'�h\u0004���{��\u001C-����dT*v�\u0007�Ԯ��i�][�.\u0018ʌ�\u001AG��(", "role" : "client", "accBalance" : 24.474105780728106", "email": someone@yahoo.com" }

From this i want to get just the username and the email to be in my view, can you help me with some ideas because i`m very new at json files, i used jackson and gson to help me with the json files methods. Thank you !

This is my controller code:


public class AdminViewBuyersController{

    private ObservableList<User> users = observableArrayList();

    @FXML
    private TableView<User> table;
    @FXML
    private TableColumn<Festival, String> user;
    @FXML
    private TableColumn<Festival, String> email;



    private void initialize() throws IOException {
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(new File("C:\\Users\\vladd\\.registration-example\\config")));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        if (br != null) {
            String st;
            while ((st = br.readLine()) != null) {
                addUsersToList(new GsonBuilder().create().fromJson(st, User.class));
            }
        }
        setTable();
    }

    private void addUsersToList(User user) {
        users.add(user);
    }

    private void setTable() {
        user.setCellValueFactory(new PropertyValueFactory<>("userName"));
        email.setCellValueFactory(new PropertyValueFactory<>("email"));
        table.setItems(users);
    }
higg
  • 1
  • 3
  • Where specifically are you stuck? Have you defined a class representing the data (e.g. a `User` class with `username`, `password`, `role`, `accBalance`, and `email` properties)? Can you create a `TableView` and display the columns you want with some mock data? Have you managed to use Jackson to parse the JSON file and create a list of objects? There are many distinct, *independent* steps in what you are asking to do; you should separate out the concerns and focus your question on a specific step when you are stuck. – James_D May 11 '20 at 20:00
  • Hi, yes, i have a User class with all the properties and getters, i have the tableview, im stuck at the fxml controller where i should implement the method to parse the json file to create the list and after to use the list to populate my table view with usernames and emails – higg May 11 '20 at 20:07
  • So post the code that you’ve tried, explain what goes wrong. – James_D May 11 '20 at 20:08
  • I post the code, the problem is that when i click the button that gets me to this view, there is just an empty tableview with nothing in it – higg May 11 '20 at 20:22
  • I don’t think you typically read a JSON file line-by-line. E.g. see [this tutorial](https://www.baeldung.com/gson-deserialization-guide), particularly part 6. – James_D May 11 '20 at 20:30

0 Answers0