How to customize the JSON for a certain class in SpringMVC

前端 未结 2 902
萌比男神i
萌比男神i 2021-01-27 08:09

I\'m using SpringMVC and have the following Method.

@RequestMapping(\"/login\")
public @ResponseBody User login(User user) {
    // some operation here ....
             


        
2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-27 08:52

    Below posting an example for deserializing json Array to Arraylist

    @RequestMapping(value = "/mapJsonObjects", method = RequestMethod.POST)
    public static ModelAndView parseJSONObjects(@RequestParam String jsonList,HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        JSONParser parser=new JSONParser();
        ArrayList filteredList = new ArrayList();
       Object obj = parser.parse(jsonList);
       JSONArray newList = (JSONArray)obj;
       filteredList = newList;
                  -----------
    

    }

    To convert an array List to json array. First add the below beans to springServlet.xml

    
        
            text/html
        
    
    

    Then from the controller return the arraylist as below

                    Map filteredMap = new LinkedHashMap();
                    List filteredList;
                    ------filteredMap logic -----------
                filteredAccountMap.put("rows", filteredList);
                return new ModelAndView("jsonView", filteredMap);
    

提交回复
热议问题