在网上看到了很多方法 使用 jsonconfig 过滤掉属性 还是不行
public void plist() {
// TODO Auto-generated method stub
List<Province> plist=pcd.plist();
JsonConfig config=new JsonConfig();
config.setJsonPropertyFilter(new PropertyFilter() {

@Override
public boolean apply(Object arg0, String arg1, Object arg2) {
// TODO Auto-generated method stub
if(arg1.equals("cities")||arg1.equals("userinfos")){
return true;
}
return false;
}
});
JSONArray json=JSONArray.fromObject(plist,config);
HttpServletResponse response = ServletActionContext.getResponse(); 
response.setCharacterEncoding("utf-8");
try {
response.getWriter().write(json.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

解决方案 »

  1.   

    <hibernate-mapping>
        <class name="com.lee.bbs.entity.Province" table="PROVINCE" schema="BBS">
            <id name="pid" type="java.lang.Integer">
                <column name="PID" precision="0" />
                <generator class="assigned" />
            </id>
            <property name="pname" type="java.lang.String">
                <column name="PNAME" length="40" not-null="true" />
            </property>
            <set name="cities" inverse="true" lazy="false">
                <key>
                    <column name="PID" precision="0" not-null="true" />
                </key>
                <one-to-many class="com.lee.bbs.entity.City" />
            </set>
            <set name="userInfos" inverse="true" lazy="false">
                <key>
                    <column name="PID" precision="0" not-null="true" />
                </key>
                <one-to-many class="com.lee.bbs.entity.UserInfo" />
            </set>
        </class>
    </hibernate-mapping>
      

  2.   

    net.sf.json.JSONException: There is a cycle in the hierarchy!
    at net.sf.json.util.CycleDetectionStrategy$StrictCycleDetectionStrategy.handleRepeatedReferenceAsObject(CycleDetectionStrategy.java:97)
    at net.sf.json.JSONObject._fromBean(JSONObject.java:657)
    at net.sf.json.JSONObject.fromObject(JSONObject.java:172)
    at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:274)
    at net.sf.json.JSONArray._processValue(JSONArray.java:2513)
    at net.sf.json.JSONArray.processValue(JSONArray.java:2538)
    at net.sf.json.JSONArray.addValue(JSONArray.java:2525)
    at net.sf.json.JSONArray._fromCollection(JSONArray.java:1056)
    at net.sf.json.JSONArray.fromObject(JSONArray.java:123)
    at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:240)
    at net.sf.json.JSONObject._processValue(JSONObject.java:2655)
    at net.sf.json.JSONObject.processValue(JSONObject.java:2721)
    at net.sf.json.JSONObject.setInternal(JSONObject.java:2736)
    at net.sf.json.JSONObject.setValue(JSONObject.java:1424)
    at net.sf.json.JSONObject.defaultBeanProcessing(JSONObject.java:765)
    at net.sf.json.JSONObject._fromBean(JSONObject.java:699)
    at net.sf.json.JSONObject.fromObject(JSONObject.java:172)
    at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:274)
    at net.sf.json.JSONObject._processValue(JSONObject.java:2655)
    at net.sf.json.JSONObject.processValue(JSONObject.java:2721)
    at net.sf.json.JSONObject.setInternal(JSONObject.java:2736)
    at net.sf.json.JSONObject.setValue(JSONObject.java:1424)
    at net.sf.json.JSONObject.defaultBeanProcessing(JSONObject.java:765)
    at net.sf.json.JSONObject._fromBean(JSONObject.java:699)
    at net.sf.json.JSONObject.fromObject(JSONObject.java:172)
    at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:274)
    at net.sf.json.JSONArray._processValue(JSONArray.java:2513)
    at net.sf.json.JSONArray.processValue(JSONArray.java:2538)
    at net.sf.json.JSONArray.addValue(JSONArray.java:2525)
    at net.sf.json.JSONArray._fromCollection(JSONArray.java:1056)
    at net.sf.json.JSONArray.fromObject(JSONArray.java:123)
    at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:240)
    at net.sf.json.JSONObject._processValue(JSONObject.java:2655)
    at net.sf.json.JSONObject.processValue(JSONObject.java:2721)
    at net.sf.json.JSONObject.setInternal(JSONObject.java:2736)
    at net.sf.json.JSONObject.setValue(JSONObject.java:1424)
    at net.sf.json.JSONObject.defaultBeanProcessing(JSONObject.java:765)
    at net.sf.json.JSONObject._fromBean(JSONObject.java:699)
    at net.sf.json.JSONObject.fromObject(JSONObject.java:172)
    at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:274)
    at net.sf.json.JSONArray._processValue(JSONArray.java:2513)
    at net.sf.json.JSONArray.processValue(JSONArray.java:2538)
    at net.sf.json.JSONArray.addValue(JSONArray.java:2525)
    at net.sf.json.JSONArray._fromCollection(JSONArray.java:1056)
    at net.sf.json.JSONArray.fromObject(JSONArray.java:123)
    at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:240)
    at net.sf.json.JSONObject._processValue(JSONObject.java:2655)
    at net.sf.json.JSONObject.processValue(JSONObject.java:2721)
    at net.sf.json.JSONObject.setInternal(JSONObject.java:2736)
    at net.sf.json.JSONObject.setValue(JSONObject.java:1424)
    at net.sf.json.JSONObject.defaultBeanProcessing(JSONObject.java:765)
    at net.sf.json.JSONObject._fromBean(JSONObject.java:699)
    at net.sf.json.JSONObject.fromObject(JSONObject.java:172)
    at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:274)
    at net.sf.json.JSONArray._processValue(JSONArray.java:2513)
    at net.sf.json.JSONArray.processValue(JSONArray.java:2538)
    at net.sf.json.JSONArray.addValue(JSONArray.java:2525)
    at net.sf.json.JSONArray._fromCollection(JSONArray.java:1056)