0

I'm trying to convert a the following string so that I can get the properties out of it. I'm trying to insert these in the DB by getting their properties and then making objects out of them

[{"ParkingSpaces;;;;":"Name;CarPlate;Description;ExpirationDate;Owner"},{"ParkingSpaces;;;;":"A5;T555;Parkingspace A5;;"},{"ParkingSpaces;;;;":"A6;T666;Parkingspace A6;;"},{"ParkingSpaces;;;;":"A7;T777;Parkingspace A7;;"},{"ParkingSpaces;;;;":""}]

I got this string from a CSV file.

Anyone who has an idea on how I can approach this?

Thanks in advance.

Cootri
  • 3,806
  • 20
  • 30
Mathias Schrooten
  • 722
  • 2
  • 11
  • 20

2 Answers2

0

Your code is quite messy, but it's doable. You can either use simple JSON parsing method like in the example:

 final String json = "[{\"ParkingSpaces;;;;\":\"Name;CarPlate;Description;ExpirationDate;Owner\","
{\"ParkingSpaces;;;;\":\"A5;T555;Parkingspace A5;;\"},{\"ParkingSpaces;;;;\":\"A6;T666;Parkingspace A6;;\"},{\"ParkingSpaces;;;;\":\"A7;T777;Parkingspace A7;;\"},{\"ParkingSpaces;;;;\":\"\"}]";
        final org.json.JSONArray jSONArray = new JSONArray(json);
        for (int i = 0; i < jSONArray.length(); i++) {
            final org.json.JSONObject jSONObject = jSONArray.getJSONObject(i);
            final String parkingSpaces = jSONObject.getString("ParkingSpaces;;;;");
            final String spaces[] = parkingSpaces.split(";");
            System.out.println(Arrays.toString(spaces));
        }
    }

or use some bindings like Jackson.

zacheusz
  • 8,750
  • 3
  • 36
  • 60
0

What you have there is JSON with some semicolon separated strings in it. I wouldn't call this a CSV format at all.

You could parse the JSON to Java objects with a JSON parser like Gson, but you'll still need to pick the "columns" out of the Java object since they are not properly defined in JSON.

Something like this should work, I recommend you add more error checking than I have though:

public class DBEntry {

    @SerializedName("ParkingSpaces;;;;")
    @Expose
    private String ParkingSpaces;

    public String getParkingSpaces() {
        return ParkingSpaces;
    }

    public void setParkingSpaces(String ParkingSpaces) {
        this.ParkingSpaces = ParkingSpaces;
    }

}

public static void main(String[] args) {
    String json = "[{\"ParkingSpaces;;;;\":\"Name;CarPlate;Description;ExpirationDate;Owner\"},{\"ParkingSpaces;;;;\":\"A5;T555;Parkingspace A5;;\"},{\"ParkingSpaces;;;;\":\"A6;T666;Parkingspace A6;;\"},{\"ParkingSpaces;;;;\":\"A7;T777;Parkingspace A7;;\"},{\"ParkingSpaces;;;;\":\"\"}]";

    // Convert JSON to java objects using the popular Gson library
    Gson gson = new Gson();
    Type collectionType = new TypeToken<ArrayList<DBEntry>>(){}.getType();
    List<DBEntry> results = gson.fromJson(json, collectionType);

    boolean header = true;
    for (DBEntry result : results) {
        // Ignore the header and empty rows
        if (header || result.getParkingSpaces().isEmpty()) { header = false; continue; }

        // Grab the columns from the parking spaces string
        String[] columns = result.getParkingSpaces().split(";");

        // TODO: Store this record in your database
        System.out.println("New entry: " + StringUtils.join(columns, ", "));
    }
}
seanhodges
  • 17,426
  • 15
  • 71
  • 93