tags:

views:

47

answers:

1

Hi all, 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?

A: 

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

Aito
Thanks for the replies. Now I also think it is calling MenuAction twice, I just can not figure out where tho. I will get back once I figured something out.