Bharath Pateru
Bharath Pateru

Reputation: 99

convert list to json using Jackson

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

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

but I want to convert it as :

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

Below is my spring controller :

@RequestMapping("/getGodowns")
public @ResponseBody Map 
getGodownsBasedOnDistrict(@RequestParam(value="district_code") String 
dist_code) {

List<CscGodownBean> godown_list = null;
Map<String, List<CscGodownBean>> m = new HashMap();
String exception = null;
try
{
//getting name and codes here
godown_list = scm_service.getGodownListBesedOnDistCode(dist_code);
}catch(Exception ex)
{
ex.printStackTrace();
exception = ex.getMessage();
}

if(godown_list!=null) {
for(int i=0;i<godown_list.size();i++) {
m.put("GodownMaster",godown_list);
}
}
return m;
}

Upvotes: 4

Views: 17179

Answers (4)

Ajay Kumar
Ajay Kumar

Reputation: 3272

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.

Upvotes: 1

TOUZENE Mohamed Wassim
TOUZENE Mohamed Wassim

Reputation: 712

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.

Upvotes: 2

Vivek N
Vivek N

Reputation: 304

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

Upvotes: 0

Leffchik
Leffchik

Reputation: 2030

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

Upvotes: 3

Related Questions