convert list to json using Jackson

后端 未结 4 1187
灰色年华
灰色年华 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:33

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

    @RequestMapping("/getGodowns")
    public @ResponseBody List
    getGodownsBasedOnDistrict(@RequestParam(value="district_code") String 
    dist_code) {
    
    List godown_list = new ArrayList();
    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 godown_list = new ArrayList();
    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.

提交回复
热议问题