计算在 struts2 中生成两次的 json 对象时出现问题
我正在尝试在以下代码中获取 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许您调用了 MenuAction 两次。检查您的表单/ajax 请求。
Perhaps you're calling the MenuAction twice. Check your form/ajax request.