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

来源:互联网 发布:临床医学英国留学 知乎 编辑:程序博客网 时间:2024/05/29 12:43


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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 一个月染了6次头怎么办 五0二干在衣服上怎么办 刚怀孕见红了肚子不痛怎么办 我有外遇了老婆不离婚怎么办 套了牙套的牙疼怎么办 我鼻子上有很多螨虫和黑头怎么办 鱼刺卡在喉咙怎么办最有效的办法 脚被蚊子咬了很痒怎么办 好压7z密码忘了怎么办 4g卡显示2g网络怎么办 过塑机把纸吞了怎么办 红米1s开不了机怎么办 跟老婆吵架闹的要离婚该怎么办 充了q币没有到账怎么办 9个月宝宝吃了盐怎么办 红米4x开不了机怎么办 鱼身上有红斑像出血了怎么办 草鱼身上有红斑像出血了怎么办 宝宝屁眼红的破皮了怎么办 孩子身上起红疙瘩很痒怎么办 久而不射,但软了怎么办 盆底综合肌力1级怎么办 头发掉的厉害怎么办吃什么好 给蜂蛰了肿了痒怎么办 小米手环2没电了怎么办 小米手环2不亮了怎么办 红米3s无限重启怎么办 乐视手机1s卡顿怎么办 老公出轨了怎么办你会选择离婚吗 c盘和d盘换换了怎么办 晚上2点到3点醒怎么办 红米3s变砖了怎么办 6s锁屏密码忘了怎么办 怀孕9个月了胃疼怎么办 怀孕6个月了胃疼怎么办 孕妇胃疼怎么办4个月了 25岁欠了5万块钱怎么办 感冒嗓子疼怎么办最简单的方法 和老婆离婚了我的心好痛怎么办 4s店不给退定金怎么办 教你闪腰了后该怎么办