Struts 2 JSON bean 解析 [英] Struts 2 JSON bean parsing
本文介绍了Struts 2 JSON bean 解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试让 Struts 管理数据以这种方式解析为 JSON:
I tried to let Struts managing data to be parsed into JSON that way:
我的豆子:
public class Person implements Serializable {
private String name;
private String surname;
public Person(String name, String surname) {
this.name = name;
this.surname = surname;
}
/*
* GETTERS AND SETTERS
* ...
*/
}
我的行动:
public class action {
private List<Person> people;
private String message;
public String execute() {
this.message = "HELLO";
/*
* Initiliaze the list
*/
return SUCCESS;
}
public List<Person> getPeople() { return this.people; }
public String getMessage() { return this.message; }
}
我的 struts.xml:
My struts.xml:
<struts>
<package name="ajax-package" namespace="/ajax" extends="json-default">
<result-types>
<result-type name="myjson" class="org.apache.struts2.json.JSONResult">
<param name="noCache">true</param>
</result-type>
</result-types>
<action class="action" method="execute" name="action">
<result type="myjson">
<param name="includeProperties">
message, people\[\d+\]
</param>
</result>
</action>
</package>
</struts>
但是,如果我将一个条目放入我的列表中,它将在 JSON 中由一个具有唯一空条目的列表表示:
But, if I put an entry in my list, it is represented, in JSON, by a list with an unique empty entry:
{"message":"HELLO","people":[{}]}
它尝试使用 GSON 来序列化我的列表,但 struts 会转义引号.
It tried to use GSON to serialize my list, but struts escapes quotes.
推荐答案
我在这个帖子中找到了解决方案:
I found the solution in this thread:
<param name="includeProperties">
message, people\[\d+\]\..*
</param>
这将包括可通过 get 方法访问的所有属性.
This will include all properties that are accessible through get method.
这篇关于Struts 2 JSON bean 解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文