【问题标题】:Not able to fetch all json response from server无法从服务器获取所有 json 响应
【发布时间】:2015-06-28 11:16:09
【问题描述】:

我想从服务器获取 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 的操作中指定的所有值。

如前所述,值已正确打印,但在 JSP 中访问时,如 alert(data.atomAddressList[0].placeByCountryId.id); 会显示

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

【问题讨论】:

  • 你能把atomAddressList指定为root而不是使用includeProperties参数吗?
  • 显示null
  • 尝试使用&lt;result type="json"/&gt; w/o 参数并发布 json 响应。
  • 显示错误org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: java.lang.reflect.InvocationTargetException
  • 嗯...你确定没有任何附加参数的&lt;param name="root"&gt;atomAddressList&lt;/param&gt; 不起作用吗?

标签: java regex json jsp struts2


【解决方案1】:

您没有在 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    • 2017-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多