convert list to json using Jackson

后端 未结 4 1158
灰色年华
灰色年华 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<AppUser> 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.

    0 讨论(0)
  • 2020-12-21 02:33

    Change the return result from Map to List<CscGodownBean> and put : retrun godown_list So;

    @RequestMapping("/getGodowns")
    public @ResponseBody List<CscGodownBean>
    getGodownsBasedOnDistrict(@RequestParam(value="district_code") String 
    dist_code) {
    
    List<CscGodownBean> godown_list = new ArrayList<CscGodownBean>();
    String exception = null;
    try
    {
        //getting name and codes here
        godown_list = scm_service.getGodownListBesedOnDistCode(dist_code);
    }catch(Exception ex)
    {
       ex.printStackTrace();
       exception = ex.getMessage();
    }
    
    return godown_list ;
    }
    

    UPDATE

    And you can return result as string and you will get what you need :

    @RequestMapping("/getGodowns")
    public @ResponseBody String
    getGodownsBasedOnDistrict(@RequestParam(value="district_code") String 
    dist_code) {
    
    List<CscGodownBean> godown_list = new ArrayList<CscGodownBean>();
    String exception = null;
    try
    {
        //getting name and codes here
        godown_list = scm_service.getGodownListBesedOnDistCode(dist_code);
    }catch(Exception ex)
    {
       ex.printStackTrace();
       exception = ex.getMessage();
    }
    ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
        String arrayToJson = objectMapper.writeValueAsString(godown_list);
        System.out.println("Convert List to JSON :");
        System.out.println(arrayToJson);
    
    return arrayToJson ;
    }
    

    The returned String is json format.

    0 讨论(0)
  • 2020-12-21 02:44

    You add return type as Map, still you want the same then Just in ajaxComplete() put code;

    var response = '{"GodownMaster":[{"pname":"FCI CHARLAPALLI","pcode":"16042"}, {"pname":"MLS CIRCLE 1 L.B. NAGAR","pcode":"16016"},{"pname":"MLS CIRCLE 4 AZAMABAD","pcode":"16003"},{"pname":"MLS CIRCLE 6 VIDYANAGAR","pcode":"16005"},{"pname":"OTHERS","pcode":"1699"}]}'
    
    
    JSON.stringify(JSON.parse(response).GodownMaster);
    
    0 讨论(0)
  • 2020-12-21 02:51

    Why are you putting your list into Map? Code looks weird. If you want to return a list, just do it:

    @RequestMapping("/getGodowns")
    public @ResponseBody List<CscGodownBean> getGodownsBasedOnDistrict(@RequestParam(value="district_code") String dist_code) {
        List<CscGodownBean> godown_list = null;
        String exception = null;
        try {
            //getting name and codes here
            godown_list = scm_service.getGodownListBesedOnDistCode(dist_code);
        } catch (Exception ex) {
            ex.printStackTrace();
            exception = ex.getMessage();
        }
        return godown_list;
    }
    
    0 讨论(0)
提交回复
热议问题