0

I'm using a threaded server to get data out of an SQLite database, I have created in this case Drivers to get the various strings and ints from the database. On the server I'm creating a HashMap.

The HashMap is sent to a client handler where I create a Json Object and then override the run method to send it to the client.

On the client I'm using swing and creating buttons and tables for a GUI. When a button is pressed it calls this method which parses the reply into a Json object:

private void showDrivers() {
        if (printWriter != null && bufferedReader != null) {
            String toSend = "get";

            printWriter.println(toSend);

            String reply = null;

            statusLabel.setText("Status: wait for server to reply");
            try {
                reply = bufferedReader.readLine();
                statusLabel.setText("Status: received reply from server");
JsonParser.parseString(reply).getAsJsonObject();
            } catch (IOException ex) {
                statusLabel.setText("IOException " + ex);
            }
            System.out.println(jsonDriver);

            JsonObject jsonReply = JsonParser.parseString(reply).getAsJsonObject();

            JsonArray jsonDriverArray = jsonReply.getAsJsonArray("driverId");

            System.out.println(jsonDriverArray);
     }
}

I'm trying to get all the members from the array and then place them in a table, using the console to check that I'm actually getting the data from the array.

I have run everything in the debugger, making sure that I'm actually sending the data to the client, which I am. I expected that by using this:

JsonArray jsonDriverArray = jsonReply.getAsJsonArray("driverId");

It should get driverId 1, 2, 3, etc printing out to the console

System.out.println(jsonDriverArray);

Returns null, I thought that by using driverId as the member name it would get all the driverIds, likwise if I changed it to forenames it should get all the forenames.

In the debugger I get this which is stored in jsonReply:

1 -> {JsonObject@3072} "{"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 -> {JsonObject@3074} "{"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 -> {JsonObject@3076} "{"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"}"
4 -> {JsonObject@3078} "{"driverId":4,"driverRef":"alonso","number":"14","code":"ALO","forename":"Fernando","surname":"Alonso","dob":"29/07/1981","nationality":"Spanish","url":"http://en.wikipedia.org/wiki/Fernando_Alonso"}"
stef_axel
  • 1
  • 1
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Apr 02 '22 at 16:32
  • Try the accepted answer to [Accessing members of items in a JsonArray](https://stackoverflow.com/q/1568762/16653700). – Alias Cartellano Apr 02 '22 at 22:12

0 Answers0