计算在 struts2 中生成两次的 json 对象时出现问题

发布于 2024-08-20 19:08:48 字数 1722 浏览 6 评论 0原文

我正在尝试在以下代码中获取 JSON 对象。问题是,我得到了两次对象。任何帮助表示赞赏。

public class MenuAction {
    private String ms;
    private List<Menu> menus;

    public String getMs() {
        return ms;
    }

    public void setMs(String ms) {
        this.ms = ms;
    }

    //execute method
    public String execute(){
        menus = new ArrayList<Menu>();
        Menu af = new Menu();
        af.setText("A Folder");
        af.setCls("folder");
        af.setLeaf(false);
        af.setId(10);
        menus.add(af);

        List<Menu> aList = new ArrayList<Menu>();
        Menu menu;
        menu = new Menu();
        menu.setText("A Child 1");
        menu.setCls("file");
        menu.setLeaf(true);
        menu.setId(11);
        aList.add(menu);
        menu = new Menu();
        menu.setText("A Child 2");
        menu.setCls("file");
        menu.setLeaf(true);
        menu.setId(12);
        aList.add(menu);
        af.setChildren(aList);

        JSONArray ja = JSONArray.fromObject(menus);
        try {
            ms = ja.toString();
        } catch (Exception e){
            ms = "ss";
        }
        System.out.println(ms);
        return Action.SUCCESS;
    }
} 

它打印的内容如下。

[{"children":[{"children":[],"cls":"file","id":11,"leaf":true,"text":"A Child 1"},{"children":[],"cls":"file","id":12,"leaf":true,"text":"A Child 2"}],"cls":"folder","id":10,"leaf":false,"text":"A Folder"}]

[{"children":[{"children":[],"cls":"file","id":11,"leaf":true,"text":"A Child 1"},{"children":[],"cls":"file","id":12,"leaf":true,"text":"A Child 2"}],"cls":"folder","id":10,"leaf":false,"text":"A Folder"}]

基本上它会生成相同的 json 字符串两次。为什么要这样做呢?

I am trying to obtain JSON object in the following code. The problem is, I am getting the object twice. Any help is appreciated.

public class MenuAction {
    private String ms;
    private List<Menu> menus;

    public String getMs() {
        return ms;
    }

    public void setMs(String ms) {
        this.ms = ms;
    }

    //execute method
    public String execute(){
        menus = new ArrayList<Menu>();
        Menu af = new Menu();
        af.setText("A Folder");
        af.setCls("folder");
        af.setLeaf(false);
        af.setId(10);
        menus.add(af);

        List<Menu> aList = new ArrayList<Menu>();
        Menu menu;
        menu = new Menu();
        menu.setText("A Child 1");
        menu.setCls("file");
        menu.setLeaf(true);
        menu.setId(11);
        aList.add(menu);
        menu = new Menu();
        menu.setText("A Child 2");
        menu.setCls("file");
        menu.setLeaf(true);
        menu.setId(12);
        aList.add(menu);
        af.setChildren(aList);

        JSONArray ja = JSONArray.fromObject(menus);
        try {
            ms = ja.toString();
        } catch (Exception e){
            ms = "ss";
        }
        System.out.println(ms);
        return Action.SUCCESS;
    }
} 

what it prints is the following.

[{"children":[{"children":[],"cls":"file","id":11,"leaf":true,"text":"A Child 1"},{"children":[],"cls":"file","id":12,"leaf":true,"text":"A Child 2"}],"cls":"folder","id":10,"leaf":false,"text":"A Folder"}]

[{"children":[{"children":[],"cls":"file","id":11,"leaf":true,"text":"A Child 1"},{"children":[],"cls":"file","id":12,"leaf":true,"text":"A Child 2"}],"cls":"folder","id":10,"leaf":false,"text":"A Folder"}]

Basically it is producing same json string twice. Why is it doing so?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花之痕靓丽 2024-08-27 19:08:48

也许您调用了 MenuAction 两次。检查您的表单/ajax 请求。

Perhaps you're calling the MenuAction twice. Check your form/ajax request.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文