2015-04-21 58 views
2

所有JSON響應我要來從服務器,以JSON響應我使用下面的代碼無法擷取來自服務器

public class AtomAddressDetail implements java.io.Serializable { 

    private Long id; 
    private Place placeByStateId; 
    private Atom atom; 
    private Place placeByCountryId; 
    private Place placeByCityId; 
    private Place placeByStreetId; 
    private Place placeByAreaId; 
    private String houseno; 
    //getter and setter 
    } 

public class Place implements java.io.Serializable { 
    private Long id; 
    private String name; 
    private String about; 
    //getter and setter 
} 

在行動

public class SettingAction extends ActionSupport { 
private long pageId; //getter and setter 
private long id; //getter and setter 
private List<AtomAddressDetail> atomAddressList; 
    public String singleAddress() { 
     setAtomAddressList(cdao.singleAddress(getId(), getPageId())); 
     for (AtomAddressDetail a : getAtomAddressList()) { 
          System.out.println("Country " + a.getPlaceByCountryId().getId() + " " + a.getPlaceByCountryId().getName()); 
          System.out.println("state " + a.getPlaceByStateId().getId() + " " + a.getPlaceByStateId().getName()); 
          System.out.println("city " + a.getPlaceByCityId().getId() + " " + a.getPlaceByCityId().getName()); 
          System.out.println("area " + a.getPlaceByAreaId().getId() + " " + a.getPlaceByAreaId().getName()); 
          System.out.println("street " + a.getPlaceByStreetId().getId() + " " + a.getPlaceByStreetId().getName()); 
         } 
      } 
     public List<AtomAddressDetail> getAtomAddressList() { 
       return atomAddressList; 
      } 

      public void setAtomAddressList(List<AtomAddressDetail> atomAddressList) { 
       this.atomAddressList = atomAddressList; 
      } 
} 

輸出:

Country 2 India 
state 3 asdf 
city 4 sdfsd 
area 5 www 
street 6 sdfdsa f 

In struts.xml

<action name="SingleAddressDetail" class=".SettingAction" method="singleAddress"> 
      <result name="success" type="json"> 
       <param name="includeProperties"> 
        ^atomAddressList\[\d+\]\.id, 
        ^atomAddressList\[\d+\]\.houseno, 
        ^atomAddressList\[\d+\]\.placeByAreaId.id, 
        ^atomAddressList\[\d+\]\.placeByAreaId.name, 
        ^atomAddressList\[\d+\]\.placeByCityId.id, 
        ^atomAddressList\[\d+\]\.placeByCityId.name, 
        ^atomAddressList\[\d+\]\.placeByStateId.id, 
        ^atomAddressList\[\d+\]\.placeByStateId.name, 
        ^atomAddressList\[\d+\]\.placeByCountryId.id, 
        ^atomAddressList\[\d+\]\.placeByCountryId.name 
       </param> 
       <param name="excludeNullProperties">true</param> 
       <param name="root"> 
        #action 
       </param> 
      </result> 
      <result name="input" type="json"/> 
      <result name="login" type="json"></result> 
     </action> 

在JSP

{"atomAddressList":[{"houseno":"sadf sadf ","id":1}]} 

問題是在JSP頁面中我得到只有兩個filds價值,但我想獲取了在struts.xml的操作中指定的所有值。

值將被打印在正確的行動提到,但在JSP訪問像alert(data.atomAddressList[0].placeByCountryId.id);它顯示

error:Uncaught TypeError: Cannot read property 'id' of undefined 
+0

你能指定'atomAddressList'作爲'root'而不是'includeProperties'參數嗎?顯示'null'的 –

+0

。 – xrcwrn

+0

用''w/o參數併發布json響應。 –

回答

0

您還沒有包含在JSON結果一些性質,因爲它們應該是每個屬性有效的正則表達式的表達式。週期字符應該逃脫。

<param name="includeProperties"> 
    ^atomAddressList\[\d+\]\.id, 
    ^atomAddressList\[\d+\]\.houseno, 
    ^atomAddressList\[\d+\]\.placeByAreaId\.id, 
    ^atomAddressList\[\d+\]\.placeByAreaId\.name, 
    ^atomAddressList\[\d+\]\.placeByCityId\.id, 
    ^atomAddressList\[\d+\]\.placeByCityId\.name, 
    ^atomAddressList\[\d+\]\.placeByStateId\.id, 
    ^atomAddressList\[\d+\]\.placeByStateId\.name, 
    ^atomAddressList\[\d+\]\.placeByCountryId\.id, 
    ^atomAddressList\[\d+\]\.placeByCountryId\.name 
</param> 
+0

是的,這是工作 – xrcwrn