无法从服务器获取所有json响应

时间:2015-04-21 10:35:08

标签: java regex json jsp struts2

我想从服务器获取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

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动作中指定的所有值。

如上所述正确地打印值,但是在正在显示<{p}>的alert(data.atomAddressList[0].placeByCountryId.id);之类的JSP中进行访问

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

1 个答案:

答案 0 :(得分: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>