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