jQuery.noConflict();   
jQuery(document).ready(function () {
     /*
     * 加载名单返回类型json—list
     * @param {Object} data
     */
    var url = "aa.action";
    var params;
    jQuery.getJSON(url,params,function callback(data){
     alert("2");//返回不了
      jQuery.each(data.items,function(i,item){
     jQuery("#maindiv").append(item.cname);
    });//这句有没有问题,我参照jquery+ajax帮助文档写的
    })
});
<div id="maindiv"></div>    
package com.tyut.actions;
import java.util.List;
import net.sf.json.JSONArray;
import com.opensymphony.xwork2.Action;
import com.tyut.domain.Candiate;
import com.tyut.service.CandiateService;public class CandiateAction { private Candiate candiate;

public Candiate getCandiate() {
return candiate;
}

private JSONArray resulttype;
public JSONArray getResulttype() {
return resulttype;
} public void setResulttype(JSONArray resulttype) {
this.resulttype = resulttype;
} public void setCandiate(Candiate candiate) {
this.candiate = candiate;
}

public String aa(){
CandiateService candiateService = new CandiateService();
List<Candiate> candiateList = candiateService.getList();     /*for (Iterator iter = candiateList.iterator(); iter.hasNext();) {          Candiate candiate = (Candiate) iter.next();  
       System.out.println(candiate.getCname());       }  */
JSONArray jsarry=new JSONArray();  
jsarry=JSONArray.fromObject(candiateList);  

resulttype=jsarry;     
return Action.SUCCESS;
}
}        <package name="aa" namespace="/" extends="json-default">
    <action name="aa" class="com.tyut.actions.CandiateAction" method="aa">
       <result type="json">
          <param name="root">resulttype</param>
       </result>
    </action>
</package>

解决方案 »

  1.   

    前面的都不需要看了,问题已经解决了,现在就是取json里面得值的问题jQuery(document).ready(function () {
         /*
         * 加载名单返回类型json—list
         * @param {Object} data
         */
        var url = "aa.action";
        var params;
        jQuery.getJSON(url,params,function callback(data){
         alert("2");
          jQuery.each(data,function(i,item){
         jQuery("#maindiv").append(item.cname);
        });
        })
    });
    <div id="maindiv"></div> 服务器端已经取到了json值,返回的json也正确,现在就是
        jQuery.each(data,function(i,item){
         jQuery("#maindiv").append(item.cname);第一个是cid,第二个是cname都是从数据库取的
        });
    显示不出来,大神看下是不是写错了。
    firebug显示的结果
    GET http://localhost:8080/zxtp/aa.action 200 OK 28ms
    jquery....min.js (第 4 行)
    头信息响应JSON
    [["1","姚林志"],["2","刘家赢"],["3","郭宇朦"],["4","李紫凝"],["5","安慧昱"],["6","魏成坤"],["7","樊攀峰"],["8","马越"]]
      

  2.   

    你这个是数组又不是键值对item[0] -> ["1","姚林志"]
    item[0][1] -> "姚林志"
      

  3.   

    如果要用item.cname取值怎么取啊<package name="aa" namespace="/" extends="json-default">
            <action name="aa" class="com.tyut.actions.CandiateAction" method="aa">
               <result type="json">
                  <param name="root">resulttype</param>
               </result>
            </action>
        </package>其中Candiate实体类,cid和cname是它的两个属性,提交处理将List集合返回
    用jQuery.each(data,function(i,item){
            jQuery("#maindiv").append(item.cname+item.cid);
        });
    为什么我返回的数组,我想取每个对象的cid和cname值,不想用数组。
      

  4.   

    sf的json包我没用过,自己翻下他的文档,看那个函数是以对象形式构建json串的我用的是官方json包
    JSONArray ary = new JSONArray(List);
      

  5.   

    大哥,你返回的是二维数组呀,当然要用数组的取法,如果你要使用obj.property这种取法的话,你server端返回的数据格式就应该是[{"cid": 1, "cname": "kotori"},...]
      

  6.   

    org.json包我已经导入了,我就是想返回[{"cid": 1, "cname": "kotori"},...]这种形式的public String aa(){
    CandiateService candiateService = new CandiateService();
    List<Candiate> candiateList = candiateService.getList();                JSONArray result = new JSONArray(candiateList);
                    System.out.println(result);
    return Action.SUCCESS;
    }//getList中访问数据库是select cid,cname from Candiate;Candiate表中还有其它字段,这个没取全有没有影响啊。
    result输出结果是这样的:
    ["[Ljava.lang.Object;@1429cf1","[Ljava.lang.Object;@4d93e3","[Ljava.lang.Object;@128215d","[Ljava.lang.Object;@1a697a1","[Ljava.lang.Object;@2cd728","[Ljava.lang.Object;@1e1a0ee","[Ljava.lang.Object;@c954e","[Ljava.lang.Object;@1e5cf0"]
    该怎么实现啊,求个类似的例子
      

  7.   

    Candiate这个类没实现get函数吧
    你可以翻一下jsonobject的代码,是查找以get开头的函数来编织json map表的
      

  8.   


    谢谢你了,回答了我这么多问题。也谢谢其它的人,现在好了能返回Json对象集合,用起来很好。
      

  9.   

    action
    public String list(){
    channelDao cd = new channelDao();
    clist = cd.getList(1,10);
    return "success";
    }
    strutsxml
    <action name="c_*" class="gui.action.channelManage" method="{1}" >
    <result name="success" type="json">
    <param name="root">clist</param>
    </result>
    </action>
    jsp
    <script type="text/javascript">
    $.ajax({
    type: "POST",
    url: "c_list",
    //data: data,
    //dataType: "json",
    success: function(data){
    for(var i=0;i<data.length;i++){
    var html = "<tr><td>";
    html = html + data[i].cid+"</td><td>";
    html = html + data[i].title+"</td><td>";
    html = html + data[i].des+"</td><td>";
    html = html + data[i].logo+"</td><td>";
    html = html + data[i].son+"</td><td>";
    html = html + data[i].sort+"</td><td></tr>";
    $("#txt").append(html);
    }
    }
    });
    </script>