如何将JSON.stringify转换为java对象为json?

时间:2018-02-19 11:24:03

标签: jquery html json ajax spring-mvc

我有一个html表,我转换为json格式并发送到我的POST类型服务器。下面给出了将数据发送到URL的ajax调用。

$.ajax({
   url: "http://localhost:8090/DocVaccine",
   contentType: "application/json",
   type: 'POST',
   data: JSON.stringify(convertTableToJson()),
   success: function (data) {
       alert("DATA SENT");
   }
 });

收到数据后(以字符串格式)我只需打印以检查其格式。

 @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);
    }

这是我得到的输出。我如何将其转换为java对象?我有一个疫苗类,它定义了所有标题,如id,dueDate,dateGiven等。我需要将其转换为适当的json格式以存储到我的mongo数据库中。

 "[{\"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\"}}]"

0 个答案:

没有答案