convert list to json using Jackson

后端 未结 4 1176
灰色年华
灰色年华 2020-12-21 02:04

With the below code I have converted list to json but the format is as follows:

{\"GodownMaster\":[{\"pname\":\"FCI CHARLAPALLI\",\"pcode\":\"16042\"},
         


        
4条回答
  •  既然无缘
    2020-12-21 02:29

    Here is what I have used:

    @RequestMapping("/alluserreportJSON")
    public @ResponseBody String getusersJSON() {
        ObjectMapper objectMapper = new ObjectMapper();
        //Set pretty printing of json
        objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
        List userlist = null;
        @SuppressWarnings("unused")
        String exception = null;
        String arrayToJson = null;
        try {
            userlist = userService.findAllUsers();
            arrayToJson = objectMapper.writeValueAsString(userlist);
        } catch (Exception ex) {
            ex.printStackTrace();
            exception = ex.getMessage();
        }
        return arrayToJson;
    }
    

    Hope it helps someone. You can see it working here.

提交回复
热议问题