2013-07-25 37 views
1

我試圖讓Struts的管理數據被解析成JSON這樣:Struts 2的JSON豆解析

我的豆:

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

我的支柱。 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,通過列表和一個特定的空項:

{"message":"HELLO","people":[{}]} 

它試圖用GSON連載我的名單,但支柱逃脫的報價。

+0

你如何初始化列表?爲什麼不在'includeProperties'中只有'message,people'?請注意,不要爲類使用小寫名稱。 – orique

+0

我的清單初始化如下: 'people = new ArrayList (); people.add(新人(「John」,「Doe」)); ...' 如果我在此列表上執行System.out,則每個條目都會正確顯示。 – Florent06

回答

1

我找到了解決方案this thread

<param name="includeProperties"> 
    message, people\[\d+\]\..* 
</param> 

這將包括那些通過get方法可以訪問所有屬性。