I have a html table that I am converting to json format and sending to my POST type server. Given below is the ajax call to send the data to the url.
$.ajax({
url: "http://localhost:8090/DocVaccine",
contentType: "application/json",
type: 'POST',
data: JSON.stringify(convertTableToJson()),
success: function (data) {
alert("DATA SENT");
}
});
On receiving the data, (which is in string format) i simply print to check its format.
@RequestMapping(
value = "/DocVaccine",
method=RequestMethod.POST, headers = "Accept=*/*",
produces = "application/json", consumes="application/json")
public void UpdateVaccineCard(@RequestBody String p1) throws IOException
{
LOGGER.info(p1);
}
this is the output i get. How do i convert this into java object? I have a vaccine class which defines all the headers such as id, dueDate, dateGiven, etc. I need to convert this into proper json format to store into my mongo database.
"[{\"id\":\"345678\",\"dueDate\":\"07-08-2019\",\"dateGiven\":\"07-02-2016\",\"vaccine\":\"BCG\",\"age\":\"Birth\",\"weight\":\"20\",\"heigth\":\"110\",\"placeName\":\"Hiranandani\",\"bmi\":\"0\"},
{\"id\":\"345678\",\"dueDate\":\"08-08-2019\",\"dateGiven\":\"08-02-2016\",\"vaccine\":\"HepB\",\"age\":\"Birth\",\"weight\":\"20\",\"heigth\":\"110\",\"placeName\":\"Hiranandani\",\"bmi\":\"0\"},
{\"id\":\"345678\",\"dueDate\":\"09-08-2019\",\"dateGiven\":\"09-02-2016\",\"vaccine\":\"Polio virus\",\"age\":\"Birth\",\"weight\":\"30\",\"heigth\":\"110\",\"placeName\":\"Hiranandani\",\"bmi\":\"0\"},
{\"id\":\"345678\",\"dueDate\":\"10-08-2019\",\"dateGiven\":\"10-02-2016\",\"vaccine\":\"Hib\",\"age\":\"6 weeks\",\"weight\":\"30\",\"heigth\":\"110\",\"placeName\":\"Hiranandani\",\"bmi\":\"0\"},
{\"id\":\"345678\",\"dueDate\":\"11-08-2019\",\"dateGiven\":\"11-02-2016\",\"vaccine\":\"PCV\",\"age\":\"6 weeks\",\"weight\":\"30\",\"heigth\":\"110\",\"placeName\":\"Hiranandani\",\"bmi\":\"0\"},
{\"id\":\"345678\",\"dueDate\":\"12-08-2019\",\"dateGiven\":\"12-02-2016\",\"vaccine\":\"RV\",\"age\":\"6 weeks\",\"weight\":\"30\",\"heigth\":\"110\",\"placeName\":\"Hiranandani\",\"bmi\":\"0\"},
{\"id\":\"345678\",\"dueDate\":\"13-08-2019\",\"dateGiven\":\"13-02-2016\",\"vaccine\":\"Typhoid\",\"age\":\"6 weeks\",\"weight\":\"30\",\"heigth\":\"110\",\"placeName\":\"Hiranandani\",\"bmi\":\"0\"},
{\"id\":\"345678\",\"dueDate\":\"14-08-2019\",\"dateGiven\":\"14-02-216\",\"vaccine\":\"MMR\",\"age\":\"9 months\",\"weight\":\"30\",\"heigth\":\"110\",\"placeName\":\"Hiranandani\",\"bmi\":\"0\"},
{\"id\":\"345678\",\"dueDate\":\"15-08-2019\",\"dateGiven\":\"15-02-2016\",\"vaccine\":\"Varicella\",\"age\":\"1\",\"weight\":\"30\",\"heigth\":\"110\",\"placeName\":\"Hiranandani\",\"bmi\":\"0\"},
{\"id\":\"345678\",\"dueDate\":\"16-08-2019\",\"dateGiven\":\"16-02-2016\",\"vaccine\":\"HepA\",\"age\":\"1\",\"weight\":\"30\",\"heigth\":\"110\",\"placeName\":\"Hiranandani\",\"bmi\":\"0\"},
{\"id\":\"345678\",\"dueDate\":\"17-08-2019\",\"dateGiven\":\"17-02-2016\",\"vaccine\":\"Tdap\",\"age\":\"7\",\"weight\":\"40\",\"heigth\":\"110\",\"placeName\":\"Hiranandani\",\"bmi\":\"0\"}}]"
JSONArray array = new JSONArray(str)and then loop using an indexJSONObject jobj = array.getJSONObject(i)