0

I am fairly new to Java. I have a JSON File that needs to be inserted into a JTable dynamically.

First I have copied the data of JSON File to a String and then Parsed the JSON file data. This is what I have done till now

My JSON File is

{
  "users": [
    {
      "fname": "John",
      "lname": "Snow",
      "dob": "09/08/1997",
      "phone": "01458745236",
      "password": "admin",
      "cpassword": "admin",
      "email": "jhonsnow@gmail.com",
      "id": "johnsnow007"
    },
    {
      "fname": "Arya",
      "lname": "Stark",
      "dob": "02/12/1990",
      "phone": "1458562366254",
      "password": "12345",
      "cpassword": "12345",
      "email": "aryastark@gmail.com",
      "id": "aryastark12"
    }
  ]
}

This is what I have done till now

  try{

        File file = null;
        JFileChooser chooser = new JFileChooser("C:/Users/768970/Desktop/Databases");

        chooser.setAcceptAllFileFilterUsed(false); 
        FileNameExtensionFilter restrict = new FileNameExtensionFilter("Only .json files", "json"); 
        chooser.addChoosableFileFilter(restrict);

        int result = chooser.showOpenDialog(table);
        if(result == JFileChooser.APPROVE_OPTION) {
            file = chooser.getSelectedFile();

            BufferedReader br = new BufferedReader(new FileReader(file));
            String everything = "";
            try {

                StringBuilder sb = new StringBuilder();
                String line = br.readLine();

                while (line != null) {
                    sb.append(line);
                    sb.append(System.lineSeparator());
                    line = br.readLine();

                }
                everything = sb.toString();

            }finally {
                br.close();
            }

            JSONObject jsonObject = new JSONObject(everything); 

            Set<String> keys =jsonObject.keySet();
            for(String key:keys) {
                System.out.println(jsonObject.get(key));
            }





        }else{
            JOptionPane.showMessageDialog(null, "Operation is CANCELLED :(");
        }
    }catch(Exception e){
        e.printStackTrace();
    }

The Actual output is this from the code above

[{"fname": "John","lname": "Snow","dob": "09/08/1997","phone": "01458745236","password": "admin","cpassword": "admin","email": "jhonsnow@gmail.com","id": "johnsnow007"},{"fname": "Arya","lname": "Stark","dob": "02/12/1990","phone": "1458562366254","password": "12345","cpassword": "12345","email": "aryastark@gmail.com","id": "aryastark12"}]

but I expect the same in the form of JTable

Kunal Tyagi
  • 2,341
  • 1
  • 15
  • 26
  • Put the json elements into some kind of model (ie POJO) so it can be more easily encapsulated and managed. Then you would generate a "list" of these elements and build a `TabelModel` instance to support it – MadProgrammer Apr 17 '19 at 04:42
  • Did you check this? [Fill JTable from JSON object](https://stackoverflow.com/questions/25418029/fill-jtable-from-json-object) – Rcordoval Apr 17 '19 at 06:12

0 Answers0