'Gson/Json to array java

I have got a server that is connected to an SQLite Database. From here I'm putting the data into a HashMap and then into Json to send over the a client handler.

I'm then trying to to use a swing client GUI to put the data from the server into a table. On the client side to make sure I'm receiving the data I'm printing the reply to the console.

while (true){
            count++;

            if (socket == null){
                guiClientSays("Waiting for connection to be reset...");
                synchronized (waitObject){
                    try{
                        waitObject.wait();
                    }catch (InterruptedException ex){
                        Logger.getLogger(GUIClient.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
            guiClientSays("Waiting for message" + count + " from server...");
            String reply = null;
            try{
                reply = bufferedReader.readLine();
                guiClientSays("Received \"" + reply + "\" from server.");
            }catch (IOException ex){
                guiClientSays("IOException " + ex);
            }
}

In the console I'm getting this

Client1: Received "{"1":{"driverId":1,"driverRef":"hamilton","number":"44","code":"HAM","forename":"Lewis","surname":"Hamilton","dob":"07/01/1985","nationality":"British","url":"http://en.wikipedia.org/wiki/Lewis_Hamilton"},"2":{"driverId":2,"driverRef":"heidfeld","number":"","code":"HEI","forename":"Nick","surname":"Heidfeld","dob":"10/05/1977","nationality":"German","url":"http://en.wikipedia.org/wiki/Nick_Heidfeld"},"3":{"driverId":3,"driverRef":"rosberg","number":"6","code":"ROS","forename":"Nico","surname":"Rosberg","dob":"27/06/1985","nationality":"German","url":"http://en.wikipedia.org/wiki/Nico_Rosberg"}, etc

To make sure I could populate the table I connected the GUI client to the database that worked with no issues

ArrayList<Driver> drivers = Driver.readAllDrivers();
        DefaultTableModel model = (DefaultTableModel) driverTable.getModel();

        model.setRowCount(0);
        for (Driver driver : drivers){
            model.addRow(new Object[]{
                    driver.getDriverId(),
                    driver.getDriverRef(),
                    driver.getNumber(),
                    driver.getCode(),
                    driver.getForename(),
                    driver.getSurname(),
                    driver.getDob(),
                    driver.getNationality(),
                    driver.getUrl()
            });
        }

driverTable.setModel(new DefaultTableModel(
                null,
                new String[]{"Driver ID", "Driver Ref", "Number", "Code", "Forename", "Surname", "DOB", "Nationality"
                        , "URL"}
        ));

Once I had got the data to be sent by the server I tried this:

String driverReply;
            driverReply = reply;



            String rows[] = ((String) driverReply).split("\n");
            Vector<Vector<String>> dataVector = new Vector<Vector<String>>();
            for (String row : rows) {
                row = row.trim();
                Vector<String> data = new Vector<String>();
                data.addAll(Arrays.asList(row.split(",")));
                dataVector.add(data);
            }
            Vector<String> header = new Vector<String>(9);
            header.add("Driver ID");
            header.add("Driver Ref");
            header.add("Number");
            header.add("Code");
            header.add("Forename");
            header.add("Surname");
            header.add("DOB");
            header.add("Nationality");
            header.add("URL");

The image shows the gui and how it outputs the table

GUI

I should be getting 800 rows and it should display like this:

1 hamilton 44 HAM Lewis Hamilton 07/01/1985 British http://en.wikipedia.org/wiki/Lewis_Hamilton 2 heidfeld HEI Nick Heidfeld 10/05/1977 German http://en.wikipedia.org/wiki/Nick_Heidfeld

etc



Solution 1:[1]

You can use JsonObject instead String type for var "reply".

https://docs.oracle.com/javaee/7/api/javax/json/JsonObject.html

In the client side, you can write:

            JSONParser parser = new JSONParser();

            JSONObject reply = (JSONObject) parser.parse(bufferedReader.readLine());

After that, you can set DataVector with all reply values, as follows:

            Vector<Vector<String>> dataVector = new Vector<Vector<String>>();
            reply.keySet().forEach(rowKeyStr ->
            {
               JSONObject row = (JSONObject)jsonObj.get(rowKeyStr);
               Vector<String> data = new Vector<String>();
               row.keySet().forEach(fieldKeyStr ->
               {
                  String field = (String)jsonObj.get(fieldKeyStr);
                  data.add(field);
               }
               dataVector.add(data);
            });

I hope this help you.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Isma