0

In a java WebServlet I am reading in a file:

[
{"firstName":"Chelsea","surname":"Ganter","dob":"29/03/2005","scoutNumber":"181812","joiningDate":"01/09/2016"},
{"firstName":"Oliver","surname":"Greenhatch","dob":"10/09/2007","scoutNumber":"268981","joiningDate":"17/07/2019"}
]

With:

    String[] myJsonData = request.getParameterValues("memberList");

I have tried to find out how I can read the objects to write to the database and I just can not get it to work (e.g., How to parse a JSON and turn its values into an Array?).

When I add:

    try {
        JSONObject myjson = new JSONObject(myJsonData.toString());
        JSONArray the_json_array = myjson.getJSONArray("profiles");

        int size = the_json_array.length();
        ArrayList<JSONObject> arrays = new ArrayList<JSONObject>();
        for (int i = 0; i < size; i++) {
            JSONObject another_json_object = the_json_array.getJSONObject(i);
            System.out.println(another_json_object);
                //Blah blah blah...
                arrays.add(another_json_object);
        }
    } catch (JSONException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

I get a 404 error before the application opens.

Amit Kumar Lal
  • 5,537
  • 3
  • 19
  • 37
Glyn
  • 1,933
  • 5
  • 37
  • 60
  • what do you mean by reading in ?? did you mean reading from a file and also the JSON data is from request Parameter? Also are you getting exception stack trace ? – Amit Kumar Lal Feb 22 '20 at 07:20
  • For reading in I use "String[] inputJson = request.getParameterValues("memberList");" "memberList" is passed to the WebServlet via an ajax call in a javascript. The exception stack trace is "22-Feb-2020 14:05:22.084 SEVERE [http-nio-8080-exec-5] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [client.BulkUploadMembersView] in context with path [/AwardTracker_NJE] threw exception java.lang.NullPointerException" – Glyn Feb 22 '20 at 21:55

1 Answers1

1

As per What I have understood , If you are looking to save the values into the DB by holding it into some entity class , you can directly load the JSON data into that class , I have created a sample Class called profile with the exact same fields which can be changes using @JsonPoperty
. In the sample code the date string will automatically be converted to date hence hiding the implementation and making things easier.
below is the sample code:-

import java.util.Arrays;
import java.util.Date;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.ObjectMapper;

public class SOTest {

    public static void main(String[] args) {
        try {
            String inputJson = "[\r\n" + 
                    "{\"firstName\":\"Chelsea\",\"surname\":\"Ganter\",\"dob\":\"29/03/2005\",\"scoutNumber\":\"181812\",\"joiningDate\":\"01/09/2016\"},\r\n" + 
                    "{\"firstName\":\"Oliver\",\"surname\":\"Greenhatch\",\"dob\":\"10/09/2007\",\"scoutNumber\":\"268981\",\"joiningDate\":\"17/07/2019\"}\r\n" + 
                    "]";
            com.fasterxml.jackson.databind.ObjectMapper mapper  = new ObjectMapper();

            Profile[] profiles = mapper.readValue(inputJson, Profile[].class);
            System.out.println(Arrays.asList(profiles));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class Profile{
    private String firstName;

    private String surname;

    @JsonFormat(pattern="dd/MM/yyyy")
    private Date dob;

    private int scoutNumber;

    @JsonFormat(pattern="dd/MM/yyyy")
    private Date joiningDate;

    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getSurname() {
        return surname;
    }
    public void setSurname(String surname) {
        this.surname = surname;
    }
    public Date getDob() {
        return dob;
    }
    public void setDob(Date dob) {
        this.dob = dob;
    }
    public int getScoutNumber() {
        return scoutNumber;
    }
    public void setScoutNumber(int scoutNumber) {
        this.scoutNumber = scoutNumber;
    }
    @Override
    public String toString() {
        return "Profile [firstName=" + firstName + ", surname=" + surname + ", dob=" + dob + ", scoutNumber="
                + scoutNumber + ", joiningDate=" + joiningDate + "]";
    }

}

output (just printed the ArrayList elements):-

Profile [firstName=Chelsea, surname=Ganter, dob=Tue Mar 29 05:30:00 IST 2005, scoutNumber=181812, joiningDate=Thu Sep 01 05:30:00 IST 2016], 

Profile [firstName=Oliver, surname=Greenhatch, dob=Mon Sep 10 05:30:00 IST 2007, scoutNumber=268981, joiningDate=Wed Jul 17 05:30:00 IST 2019]
Amit Kumar Lal
  • 5,537
  • 3
  • 19
  • 37
  • 1
    The write to DB is: MySQLConnection.addYMLeader(groupID, accountID, surnameAdd, firstNameAdd, dob, image, numberAdd, startDate, endDate, sectionDetails, "0", null, ""); – Glyn Feb 22 '20 at 21:56