我想从服务器获取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页面中,我只得到两个字段值,但我想获取在struts.xml
的操作中指定的所有值。
正如前面提到的那样,值在实际操作中被正确地打印出来,但是在像alert(data.atomAddressList[0].placeByCountryId.id);
这样的JSP中访问时,它会显示出来
error:Uncaught TypeError: Cannot read property 'id' of undefined
1条答案
按热度按时间s4chpxco1#
您没有在json结果中包含一些属性,因为它们应该是每个属性的有效正则表达式。应转义句点字符。