0

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\"}}]"
6
  • use gson library github.com/google/gson Commented Feb 19, 2018 at 11:26
  • 1
    Possible duplicate of Convert JSON object to Java bean using org.json package Commented Feb 19, 2018 at 11:35
  • I found this online for the suggestion given. Now how do i convert that array i have received to single lines? String json = "{\"id\":2,\"name\":\"Effective Java\",\"author\":\"Joshua Bloch\"}"; Gson gson = new Gson(); JsonElement element = gson.fromJson(json, JsonElement.class); JsonObject jsonObject = element.getAsJsonObject() Commented Feb 19, 2018 at 11:37
  • @Ele How do i separate out each line? Ive received an array in string form. How do i get each document out separately? Commented Feb 19, 2018 at 11:40
  • 1
    @KeshviSrivastava Using JSON API: JSONArray array = new JSONArray(str) and then loop using an index JSONObject jobj = array.getJSONObject(i) Commented Feb 19, 2018 at 11:47

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.