2015-05-26 61 views
2

我有一個具有3個屬性的操作類。我使用Struts2-Json插件來序列化動作類。我能夠序列化字符串selectedCompany按我的預期使用Struts2-Json插件時的Json序列化問題

問題

人民ArrayList<Person>屬性序列與空值。我似乎沒有找到我犯了錯誤的地方。

Json_Response:

enter image description here

動作類別:

package json; 

import java.util.ArrayList; 

import com.google.gson.Gson; 
import com.opensymphony.xwork2.ActionSupport; 
import com.opensymphony.xwork2.Preparable; 

import dao.DataAccess; 

public class Json extends ActionSupport implements Preparable { 

    private static final long serialVersionUID = -8415223624346993447L; 
    private ArrayList<String> list; 
    private String selectedCompany = "Buhin Engineers"; 
    private ArrayList<Person> people; 

    public ArrayList<String> getList() { 
     return list; 
    } 
    public void setList(ArrayList<String> list) { 
     this.list = list; 
    } 
    public String getSelectedCompany() { 
     return selectedCompany; 
    } 
    public void setSelectedCompany(String selectedCompany) { 
     this.selectedCompany = selectedCompany; 
    } 
    public ArrayList<Person> getPeople() { 
     return people; 
    } 
    public void setPeople(ArrayList<Person> people) { 
     this.people = people; 
    } 
    public String execute(){ 
     list = new ArrayList<String>(); 
     list.add("Yamaha"); 
     list.add("Hero Honda"); 
     return SUCCESS; 
    } 
    @Override 
    public void prepare() throws Exception { 
     // TODO Auto-generated method stub 
     populatePeople(); 
    } 
    private void populatePeople() { 
     // TODO Auto-generated method stub 
     DataAccess da = new DataAccess(); 
     setPeople(da.retrievePeople()); 

    } 
} 

struts.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE struts PUBLIC 
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 
    "http://struts.apache.org/dtds/struts-2.0.dtd"> 
    <struts> 
     <constant name="struts.devMode" value="true" /> 
     <package name="default" namespace="/" extends="json-default"> 
      <action name="Json" class="json.Json"> 
       <result type="json"> 
        <param name="includeProperties">selectedCompany,people,list</param> 
       </result> 
      </action> 
     </package> 
    </struts> 
+3

可能重複的[Struts 2的JSON豆解析](http://stackoverflow.com/q/17852550/1700321) –

回答

2

List作爲數組發佈,因此您需要定義要包含的數組。像

people\[\d+\]\..*,list\[\d+\]\..* 

如果List是一個對象(而不是簡單String),你可以縮小你的結果等來選擇屬性類型:

employee\[\d+\]\.lName,employee\[\d+\]\.fName, 

如果List對象具有內在的對象,你可以做:

//The employee\[\d+\]\.address.addressline1 is not enough !! 
//May be one can suggest better idea here :) 
employee\[\d+\]\.address,employee\[\d+\]\.address.addressline1 
+1

和註釋:http://stackoverflow.com/a/21984750/1654265 –