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);
}
}
......
......
- ajax传json的方法(附list转json方法)
- JSON学习(附:遍历JSON数据的方法)
- ajax中JSON.stringify()和JSON.parse()方法的使用
- ajax中使用JSON的方法解析
- ajax解析Json数据的方法
- 【Ajax】的一些基本方法,Json操作
- ajax解析json的两种方法
- 前台使用ajax 调用action方法,返回一个对象List集合的json
- $.ajax方法(传递json数据)
- $.ajax json数据传递方法
- $.ajax json数据传递方法
- $.ajax json数据传递方法
- $.ajax json数据传递方法
- AJAX处理JSON数据方法
- javascript $.ajax json list
- 使用JSON的方法
- 生成JSON的方法
- 使用JSON的方法
- 机器学习中的算法(1)-决策树模型组合之随机森林与GBDT
- Cannot find class 'org.junit.Test' on project build path
- Eclipse如何创建并部署web project
- XAMPP+TestLink安装步骤
- Android教程-03 常见布局的总结
- ajax传json的方法(附list转json方法)
- 直接插入排序
- 关于图像特征提取
- 粒子群优化PSO
- 【转】最值得你所关注的10个C语言开源项目
- hdu 5476
- python 操作 编辑器
- 通过UIView 来获取 Controller
- 隐马尔可夫模型攻略