ajax传json的方法(附list转json方法)


1.将您的js方法写入<script></script>中,在<script>中写的东西默认是页面加载的时候就进入,所以一般都我会在调试的时候先在方法外alert下试试看是否进入script,因为


很多时候都会出现括号对齐的问题,冏~


ajax必须要写的几个参数:type,url,data,datatype,type是请求方式,url就是你的action名字,datatype可以写json也可以是其他的啥,一般都是json。写完以后可以在


 success:function(json)内遍历传过来的json,我在action那边定义的是个list,list里面放了hashmap。一般都是使用$(json).each(function(index, element) 来遍历json,index


是循环的索引值,element是对象,这样就能 . 出你想要的值了。我这个页面是直接拼接了html,然后使用appendTo放入页面的元素中。




xxx.jsp:

......

......

function test(){
    $("#draw_tableid tbody tr").remove();

    var money=$('#slider-s').val();
   
    $.ajax({   
           type:"POST", //请求方式  
           url:"${base}/find_loan.action", //请求路径   
           data:"money="+money,
           dataType: 'json',   //返回值类型  
           success:function(json){
          /* alert(json[0].title/
               /* alert(json[0].username+" " json[0].password);  */   //弹出返回过来的List对象  
          var htmls1="";
               var result="";
               result+="<td width='3%' align='center'>"+"暂无项目"+"</td>";
    
  $(json).each(function(index, element) {
  var money2 = Number(money);
        var annualInterestRate2 = Number(element.annualInterestRate);
        /* var termUnit2 = Number(element.termUnit); */
        var termCount2 = Number(element.termCount);
  /*     var time1 = element.openEndTime.time;
      var time2 =element.nowDate.time; */
      /* System.currentTimeMillis() */
     
        /*  var compare =time; */
       /*  var sum = Number(moneynnualInterestRate2/100/12*termUnit2); */
/*  if(element.status2==300){ */

htmls1+="<tr id='trid"+"' >";
htmls1+="<td width='3%' align='center'>"+element.title+"</td>";
if(element.termUnit==1){
htmls1+="<td width='3%' align='center'>"+element.termCount+"天"+"</td>";

}
else{
htmls1+="<td width='3%' align='center'>"+element.termCount+"个月"+"</td>";
}
 
htmls1+="<td width='3%' align='center'><font color='red'>"+element.annualInterestRate+"%"+"</fond></td>";

if(element.productId==1){
htmls1+="<td width='3%' align='center'>"+"按月付息,到期还本"+"</td>";
}
else if(element.productId==2){
htmls1+="<td width='3%' align='center'>"+"一次性还本付息"+"</td>";
}else{
htmls1+="<td width='3%' align='center'>"+"等额本息"+"</td>";
}
 
/* htmls1+="<td width='3%'>"; */
/*  +date_format(myDate,'%Y-%c-%d %h:%i:%s') */
/* htmls1+="<td width='3%'>"+(time1-time2); */
 /* htmls1+="<td width='3%'>"+element.openEndTime;
 htmls1+="<td width='3%'>"+myDate.toLocaleString(); */
 /* htmls1+="<td width='3%'>"+myDate.toLocaleString(); */
 
if(element.repayType==1||element.repayType==3){
htmls1+="<td width='3%' align='center'><font color='red'>"+Math.round(money2*annualInterestRate2/12*termCount2)/100+"元"+"</fond></td>"; 
}
else{
htmls1+="<td width='3%' align='center'>"+Math.round(money2*annualInterestRate2/12*termCount2/30)/100+"元"+"</td>"; 
}
   
htmls1+="<td width='3%' align='center'>"+"<a href='${base}/toLoginPage' class='btn btn-primary btn-invest-size invest-btn'>"+"立即投资"+"</a>"+"</td>";
/*  } */
/* if(element.status1==200){
htmls2+="<tr id='trid"+"' >";
htmls2+="<td width='3%'>"+element.title;
htmls2+="<td width='3%'>"+element.termUnit+"个月";
htmls2+="<td width='3%'>"+element.annualInterestRate+"%";
htmls2+="<td width='3%'>"+element.repayType;
htmls2+="<td width='3%'>"+Math.round(money2*annualInterestRate2/12*termUnit2)/100+"元";  
htmls2+="<td width='3%'>"+"<a href='#'>"+"认购"+"</a>";
}  */
 
 

 });
     $('#account_balance').empty();
   /*    $('#account_balance2').empty();  */
     if(json==null||json==""){
              /*  htmlswidth='3%'>"+"暂无数据"; */
    /*  $(result).appendTo($("#account_balance")); */
     $("#account_balance").append(result);
}  
   $(htmls1).appendTo($("#account_balance"));
/*    alert(result);
  $(result).appendTo($("#account_balance")); */
  /*    $(htmls2).appendTo($("#account_balance2"));  */
                 
 
          }
       }) ;  
       $("#draw_tableid2 tbody tr").remove();

......

......


2.action那边你要做的事就是将你处理后得到的list转成json,但是要使用fromObject方法转化list到json需要引入几个重要的包:commons-beanutils-1.8.0.jar,


commons-collections-3.2.1.jar,commons-lang-2.5.jar,commons-logging-1.1.1.jar,ezmorph-1.0.6.jar,json-lib-2.4-jdk13.jar(这SB博客不能传文件,不能帮大

家上传了,见谅~)


大家在调试的时候可以试着先在控制台输出这个json看看,使用System.out.println("JSon字符串**********************************"+jsonArray.toString());可以打印出json


xxxaction.java:


......

......

public void tofind_loan() throws Exception {
String money= request.getParameter("money");


List<HashMap<String, Object>> moneylist = new ArrayList<HashMap<String, Object>>();


moneylist = loanService.getmoneyBox2(money);


JSONArray jsonArray = JSONArray.fromObject(moneylist);   


/*System.out.println("JSon字符串8**********************************"+jsonArray.toString());*/


writeJsonToClient(jsonArray.toString());
}

......

......


public void writeJsonToClient(String msg) {
response.setContentType("text/json");
response.setCharacterEncoding("utf-8");
try {
response.getWriter().write(msg);
response.getWriter().flush();
response.getWriter().close();
} catch (IOException e) {
logger.error("网络异常", e);
}
}

......

......


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值