【问题标题】:JQuery JQGrid: How to simplify JSON output via Spring MVCJQuery JQGrid:如何通过 Spring MVC 简化 JSON 输出
【发布时间】:2010-11-18 05:46:36
【问题描述】:

如何生成 JqGrid 所需的相同 JSON 格式:

现在我的 Spring Controller 能够产生以下 JSON 输出:

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

这是产生该输出的 Spring Controller 方法:

@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.getFirstName());
        rowJson.setCell(userRoleDTO.getLastName());

        rowJsonList.add(rowJson);
    }

    usersJsonDTO.setRows(rowJsonList);

    return usersJsonDTO;
}

这里是 UsersJsonDTO:

public class UsersJsonDTO {

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

    ...getters/setters etc...
}

这是 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);
    }


}

这些是生成我在本问题开头给出的示例输出所需的类。 @ResponseBody 自动将返回的对象转换为 JSON。见Spring Ajax Simplifications 3.0

我想要一个更干净、更简单的实现。我想要这样的东西(当然,我试过这个但它没有给出正确的输出):

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

有什么想法吗?感谢您的宝贵时间。

我也希望能够输出以下格式:

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

但是,当我尝试这样做时,我得到了以下额外的花括号(在单元格和 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"}]}
        ]
}

很多问题,但我认为它们是相关的。

【问题讨论】:

    标签: jquery json spring jqgrid response


    【解决方案1】:

    我也希望能够输出以下格式:

    这是无效的 JSON。具体如下:

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

    [] 表示数组字面量,而 JSON 数组是列表,它们只能包含数字索引。如果你想要一个哈希(又名 map 等),你可以使用字符串键,那么你必须使用一个对象,这就是为什么 {},对象文字,不断被插入。

    【讨论】:

    • 感谢您的回复。如果下面的 JSON 无效:{"id":"1","cell":["id":"1","name":"john","lastname":"smith"]},为什么会这样JQuery JqGrid 所需的格式? Spring 正确生成了以下格式: {"id":"1","cell":[{"id":"1","name":"john","lastname":"smith"}]} ,
    • 但这不是 jqGrid 所需的格式!你没有仔细研究他们的例子。如果您打算非常频繁地使用 JSON,我建议您阅读 json.org。
    【解决方案2】:

    我找到了解决方案。首先让我提供我是如何得到答案的。我正在阅读一篇文章jQuery Grid Data with jqGrid by Jack Altiere。他指出了一个指向我之前已经阅读过的 JqGrid wiki 的链接。然后他还指出了另一个链接到这个 JSON Data 包括 JqGrid 期望的 JSON 格式的信息。我认为这是官方文档的副本,但由于字体较大,看起来更容易。

    您需要做的是在 JqGrid Javascript 格式上,您需要将重复项设置为 false。这将允许您使用以下格式:

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

    我的 Spring Controller 实现:

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

    我是否达到了我的要求。绝对是的!案件已结案:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-03
      • 2017-07-29
      • 2015-11-27
      • 1970-01-01
      • 1970-01-01
      • 2016-09-06
      • 1970-01-01
      • 2011-07-20
      相关资源
      最近更新 更多