用ajax的时候
private List<Floor> floors;
其中Floor 里面有private Land land; private Set<House> Houses = new HashSet<House>(0);

JSONArray jsonArray=JSONArray.fromObject(floors);
的时候就出错了,如果是Floor类里面都是单纯的(Integer,String)字段就可以出来结果,这种情况该怎么解决啊!

解决方案 »

  1.   


    谢谢,没异常,就是不起作用,我在回调函数里加了alert,如果是纯字段就触发了,有对象的话就不触发!
      

  2.   

    你debug一下,看看JSONArray jsonArray=JSONArray.fromObject(floors);之后jsonArray是什么内容?另外,你JS端eval了吗?
      

  3.   

    还有,floors里面保存对象land
    那你是怎么取值的呢?land.xxx,是这样吗?
      

  4.   

    你把返回后的结果 在FF 或者IE里面看一下是什么格式的我在回调函数里加了alert,如果是纯字段就触发了,有对象的话就不触发!你的意思是
    如果返回的json里面有包括对象{}这种类型就有错?
      

  5.   


    不是,是实体类里面再包含类的话就根本不会传到前台,所以回调函数也就无从被调用!
    public class Land{
        private String name;
    }public class Floor{
        private Land land;//加上这个就不可以了
        private String year;//如果只有这个字段的话,可以
    }
      

  6.   


    eval 了。如果是纯String 字段的话 返回的是 [{"",""},{"",""}]这种格式,可以取到值,
    加上private Land land;就传不到前台了,
    走到JSONArray jsonArray=JSONArray.fromObject(floors);就不动了,一直查询,是不是我的关系设置的不对呢?
      

  7.   

    如果是自定义对象,把希望传递给Json的值加上getXXX()方法
      

  8.   

    别用json了,需要导入很多包,直接用xml格式返回就行了。很简单,不需要导入任何包,前台接收的时候处理也很简单。
      

  9.   

    你可以用google的 gson 这个估计可以
    比你的好用 这个gson一样是支持json的
      

  10.   


    我试试看,不是json以字串的形式传到前台的吗,还要加getXXX()吗?;
      

  11.   

    需要拼接的,
    action:
                      response.setContentType("text/xml; charset=utf-8");  
    response.setCharacterEncoding("utf-8");   
    PrintWriter pw=response.getWriter();   
    StringBuilder xml = new StringBuilder();  
                    xml.append("<items>");
    xml.append("<itembigkindid>").append(bean.getBigKindId()).append("</itembigkindid>");
    xml.append("<itemkindid>").append(bean.getKindId()).append("</itemkindid>"); 
    xml.append("<itemcontentid>").append(bean.getContentId()).append("</itemcontentid>"); 
    xml.append("<itemrow>").append(row).append("</itemrow>"); 
    xml.append("<itemcol>").append(String.valueOf(colIndex+1)).append("</itemcol>"); 
    xml.append("<itemcontentindex>").append(bean.getContentIndex()).append("</itemcontentindex>");
    xml.append("<itemcontent>").append(bean.getContent()).append("</itemcontent>"); 
    xml.append("<itemfixed>").append(row+String.valueOf(colIndex+1)).append("</itemfixed>");
    xml.append("<itemparent>").append(col).append("</itemparent>"); 
    xml.append("</items>");
            
            
            pw.print(xml.toString());  
            System.out.println(xml);
            pw.flush();
    jsp:   var items1 = data.getElementsByTagName("itembigkindid");   
                var items2 = data.getElementsByTagName("itemkindid");   
                var items3 = data.getElementsByTagName("itemcontentid");   
                var items5 = data.getElementsByTagName("itemcol"); 
                var items6 = data.getElementsByTagName("itemcontentindex"); 
                var items7 = data.getElementsByTagName("itemcontent"); 
                var items8 = data.getElementsByTagName("itemfixed");  
               
                for(var i=0;i<items1.length;i++){  
               var bigkindid = items1[i].childNodes[0].nodeValue;
               var kindid = items2[i].childNodes[0].nodeValue;
               var contentid = items3[i].childNodes[0].nodeValue;
               var row = items4[i].childNodes[0].nodeValue;
               var col = items5[i].childNodes[0].nodeValue;
               var contentIndex = items6[i].childNodes[0].nodeValue;
               var content = items7[i].childNodes[0].nodeValue;
               var fixed = items8[i].childNodes[0].nodeValue;
              
               var newNode = bigkindid+","+kindid+","+contentid+","+row+","+col+","+contentIndex;
               $("#"+fixed).append($("<option/>").attr("value",newNode).text(content));
                }
    参考一下吧。我项目中做的