开发者

JQuery JQGrid: How to simplify JSON output via Spring MVC

How to produce same JSON format that JqGrid requires:

Right now my Spring Controller is able to produce the following JSON output:

{
    "records":"5",
    "total":"20",
    "page":"1"
    "rows":[
        {"id":"1","cell":["1","john","smith"]},
        {"id":"2","cell":["2","jane","adams"]}
        ]
}

Here's the Spring Controller method that produces that output:

@RequestMapping(value = "/json", method = RequestMethod.GET)
public @ResponseBody getUsers viewUsersAsJSON() {

    logger.debug("Retrieving all users as JSON");

    UsersJsonDTO usersJsonDTO = new UsersJsonDTO();
    usersJsonDTO.setPage("1");
    usersJsonDTO.setRecords("5");
    usersJsonDTO.setTotal("20");

    ArrayList<RowJson> rowJsonList = new ArrayList<RowJson>();
    for (UserRoleDTO userRoleDTO:userRoleServiceFacade.getAll()) {  
        RowJson rowJson = new RowJson();
        rowJson.setId(userRoleDTO.getId().toString());
        rowJson.setCell(userRoleDTO.getFirstNam开发者_如何学JAVAe());
        rowJson.setCell(userRoleDTO.getLastName());

        rowJsonList.add(rowJson);
    }

    usersJsonDTO.setRows(rowJsonList);

    return usersJsonDTO;
}

Here's UsersJsonDTO:

public class UsersJsonDTO {

    private String page;
    private String total;
    private String records;
    private ArrayList<RowJson> rows;

    ...getters/setters etc...
}

Here's RowJson:

public class RowJson {

    private String id;

    private List<String> cell;

    public RowJson() {
        cell = new ArrayList<String>();
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public List<String> getCell() {
        return cell;
    }

    public void setCell(String cell) {
        this.cell.add(cell);
    }


}

Those are the classes that are needed to product the sample output I've given in the beginning of this question. The @ResponseBody automatically converts the returned object as JSON. See Spring Ajax Simplifications 3.0

I want a much cleaner and simple implementation. I want something like this (of course, I've tried this one and it doesn't give the correct output):

@RequestMapping(value = "/json", method = RequestMethod.GET)
public @ResponseBody getUsers viewUsersAsJSON() {

    logger.debug("Retrieving all users as JSON");

    UsersJsonDTO usersJsonDTO = new UsersJsonDTO();
    usersJsonDTO.setPage("1");
    usersJsonDTO.setRecords("5");
    usersJsonDTO.setTotal("20");

    usersJsonDTO.setRows(userRoleServiceFacade.getAll());

    return usersJsonDTO;
}

Any ideas? Thank you for your time.

I also like to be able to output the following format:

{
    "records":"5",
    "total":"20",
    "page":"1"
    "rows":[
        {"id":"1","cell":["id":"1","name":"john","lastname":"smith"]},
        {"id":"2","cell":["id":"2","name":"jane","lastname":"adams"]}
        ]
}

However when I try that, I get the following extra curly braces (between cell and id):

{
    "records":"5",
    "total":"20",
    "page":"1"
    "rows":[
        {"id":"1","cell":[{"id":"1","name":"john","lastname":"smith"}]},
        {"id":"2","cell":[{"id":"2","name":"jane","lastname":"adams"}]}
        ]
}

Lots of questions but I think they're related.


I also like to be able to output the following format:

That is invalid JSON. Specifically, the following:

["id":"1","name":"john","lastname":"smith"]

A [] denotes an array literal, and JSON arrays are lists, they can only contain numerical indexes. If you want a hash (aka. map, etc.), whereby you can use string keys, then you must use an object, which would be why the {}, the object literal, keeps getting inserted.


I found the solution. First let me provide how I got the answer. I was reading an article jQuery Grid Data with jqGrid by Jack Altiere. He pointed a link to the JqGrid wiki which I've already read before. Then he also pointed another link to this JSON Data which includes info on what JSON format the JqGrid expects. I think this is a copy of the official documentation but much easier in the eyes due to the larger font.

What you need to do is on the JqGrid Javascript format, you need to set the repeatitems to false. This will allow you to use the following format:

    {
        "records":"5",
        "total":"20",
        "page":"1"
        "rows":[
            {"id":"1","name":"john","lastname":"smith"},
            {"id":"2","name":"jane","lastname":"adams"}]
   }

My Spring Controller implementation:

@RequestMapping(value = "/json", method = RequestMethod.GET)
public @ResponseBody getUsers viewUsersAsJSON() {

    logger.debug("Retrieving all users as JSON");

    UsersJsonDTO usersJsonDTO = new UsersJsonDTO();
    usersJsonDTO.setPage("1");
    usersJsonDTO.setRecords("5");
    usersJsonDTO.setTotal("20");

    usersJsonDTO.setRows(userRoleServiceFacade.getAll());

    return usersJsonDTO;
}

Did I achieve what I'm asking for. Definitely yes! Case is closed :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜