[JAVAWEB实战篇]---ajax返回json的应用
2013-11-13 10:42
239 查看
1例如:
List-->json:
Java代码List<Student>list=newArrayList<Student>();
for(inti=0;i<stus.length;i++){
list.add(i);
}
JSONArrayjsonArray=JSONArray.fromObject(stus);
System.out.println(jsonArray);
对象(Student)-->json:
Java代码Studentstudent=newStudent();
student.setAge(18);
student.setName("zhangsan");
JSONObjectjsonObject=JSONObject.fromObject(student);
System.out.println(jsonObject);
List<Student>-->json
Java代码Student[]stus=newStudent[5];
List<Student>stuList=newArrayList<Student>();
for(inti=0;i<stus.length;i++){
stus[i]=newStudent();
stus[i].setAge(20);
stus[i].setName("张三"+i);
stuList.add(stus[i]);
}
JSONObjectjsonObjectFromMap=JSONObject.fromObject(stuList);
System.out.println(jsonObjectFromMap);
举例:从数据库中查询出一组数据,用ajax传到页面上,并解析生成下拉列表项
Action中:
//获得传过来的机构ID
Stringorg_id=request.getParameter("org_id");
Connectioncon=ConnectionFactory.getInstance().getConnection();
Statementpst=null;
ResultSetrs=null;
Stringsql="";
sql="selectc.group_ID,c.group_namefromgroupinfocwherec.group_ID!='"
+org_id
+"'startwithc.group_ID='"
+org_id
+"'Connectbypriorc.group_ID=c.parent_ID";
pst=con.createStatement();
rs=pst.executeQuery(sql);
JSONArrayarr=newJSONArray();
//遍历
while(rs.next()){
JSONObjectjson=newJSONObject();
json.put("GroupId",rs.getString("group_ID"));
json.put("GroupName",rs.getString("group_name"));
arr.add(json);
}
PrintWriterout=response.getWriter();
out.print(arr.toString());
returnnull;
页面中的获取传过来的list,并进行遍历,生成下拉列表项
js中:
functionchange(obj){
varselectValue=document.getElementById('org_id').options[document.getElementById('org_id').selectedIndex].text;
document.getElementById("selectValue").value=selectValue;
//window.location="<%//=request.getContextPath()%>//statisticsAction.do?cmd=toMmsStaticsByGroup&groupId="+document.getElementById('org_id').value;
varurl='<%=request.getContextPath()%>/statisticsAction.do';
varpars='cmd=getGroupByOrg&org_id='+document.getElementById('org_id').value;
varmyAjax=newAjax.Request(url,{method:'post',parameters:pars,onComplete:_complete});
}
//请求完成的处理
function_complete(originalRequest)
{
varstatusValue=originalRequest.status;
vararr=originalRequest.responseText;
if(statusValue==200)
{
vargroup=document.getElementById("columnInfoId");
group.length=0;
group.options.add(newOption("--全部--","ALL"));
varjsonss=eval("("+arr+")");
for(vari=0;i<jsonss.length;i++){
group.options.add(newOption(jsonss[i].GroupName,jsonss[i].GroupId));
}
}
elseif(statusValue==404)
{
alert("代码出现错误,链接地址有误");
}
else
{
alert("代码出现未知错误");
}
}
页面布局中:
<TABLEwidth="95%"align=centercellpadding="0"cellspacing="0">
<TBODY>
<TR>
<TDclass="zi_3F6293"align="left"height="35"width="50">机 构:</TD>
<td>
<SELECTstyle="width:150px"name="groupId"id="org_id"value="${param.org_id}"onChange="change(this.options[this.options.selectedIndex].value)">
<c:forEachitems="${organizations}"var="org">
<c:iftest="${groupId==org['GroupId']}">
<OPTIONvalue="${org['GroupId']}"selected="selected">${org['GroupName']}</OPTION>
</c:if>
<c:iftest="${groupId!=org['GroupId']}">
<OPTIONvalue="${org['GroupId']}">${org['GroupName']}</OPTION>
</c:if>
</c:forEach>
</SELECT>
</TD>
</TR>
<TR>
<TDclass=zi_3F6293align="left"height="35"width="50">群 组:</TD>
<td>
<SELECTstyle="width:150Px"name="columnInfoId"id="columnInfoId">
<OPTIONvalue="ALL">---全部---</OPTION>
</SELECT>
</TD>
</TR>
<tr>
</table>
必须jar包:http://down.51cto.com/data/1009559
List-->json:
Java代码List<Student>list=newArrayList<Student>();
for(inti=0;i<stus.length;i++){
list.add(i);
}
JSONArrayjsonArray=JSONArray.fromObject(stus);
System.out.println(jsonArray);
List<Student> list = new ArrayList<Student>(); for (int i = 0; i < stus.length; i++) { list.add(i); } JSONArray jsonArray = JSONArray.fromObject(stus); System.out.println(jsonArray);2例如:
对象(Student)-->json:
Java代码Studentstudent=newStudent();
student.setAge(18);
student.setName("zhangsan");
JSONObjectjsonObject=JSONObject.fromObject(student);
System.out.println(jsonObject);
Student student = new Student(); student.setAge(18); student.setName("zhangsan"); JSONObject jsonObject = JSONObject.fromObject(student); System.out.println(jsonObject);3综合
List<Student>-->json
Java代码Student[]stus=newStudent[5];
List<Student>stuList=newArrayList<Student>();
for(inti=0;i<stus.length;i++){
stus[i]=newStudent();
stus[i].setAge(20);
stus[i].setName("张三"+i);
stuList.add(stus[i]);
}
JSONObjectjsonObjectFromMap=JSONObject.fromObject(stuList);
System.out.println(jsonObjectFromMap);
Student[] stus = new Student[5]; List<Student> stuList = new ArrayList<Student>(); for (int i = 0; i < stus.length; i++) { stus[i] = new Student(); stus[i].setAge(20); stus[i].setName("张三" + i); stuList.add(stus[i]); } JSONObject jsonObjectFromMap = JSONObject.fromObject(stuList); System.out.println(jsonObjectFromMap);
举例:从数据库中查询出一组数据,用ajax传到页面上,并解析生成下拉列表项
Action中:
//获得传过来的机构ID
Stringorg_id=request.getParameter("org_id");
Connectioncon=ConnectionFactory.getInstance().getConnection();
Statementpst=null;
ResultSetrs=null;
Stringsql="";
sql="selectc.group_ID,c.group_namefromgroupinfocwherec.group_ID!='"
+org_id
+"'startwithc.group_ID='"
+org_id
+"'Connectbypriorc.group_ID=c.parent_ID";
pst=con.createStatement();
rs=pst.executeQuery(sql);
JSONArrayarr=newJSONArray();
//遍历
while(rs.next()){
JSONObjectjson=newJSONObject();
json.put("GroupId",rs.getString("group_ID"));
json.put("GroupName",rs.getString("group_name"));
arr.add(json);
}
PrintWriterout=response.getWriter();
out.print(arr.toString());
returnnull;
页面中的获取传过来的list,并进行遍历,生成下拉列表项
js中:
functionchange(obj){
varselectValue=document.getElementById('org_id').options[document.getElementById('org_id').selectedIndex].text;
document.getElementById("selectValue").value=selectValue;
//window.location="<%//=request.getContextPath()%>//statisticsAction.do?cmd=toMmsStaticsByGroup&groupId="+document.getElementById('org_id').value;
varurl='<%=request.getContextPath()%>/statisticsAction.do';
varpars='cmd=getGroupByOrg&org_id='+document.getElementById('org_id').value;
varmyAjax=newAjax.Request(url,{method:'post',parameters:pars,onComplete:_complete});
}
//请求完成的处理
function_complete(originalRequest)
{
varstatusValue=originalRequest.status;
vararr=originalRequest.responseText;
if(statusValue==200)
{
vargroup=document.getElementById("columnInfoId");
group.length=0;
group.options.add(newOption("--全部--","ALL"));
varjsonss=eval("("+arr+")");
for(vari=0;i<jsonss.length;i++){
group.options.add(newOption(jsonss[i].GroupName,jsonss[i].GroupId));
}
}
elseif(statusValue==404)
{
alert("代码出现错误,链接地址有误");
}
else
{
alert("代码出现未知错误");
}
}
页面布局中:
<TABLEwidth="95%"align=centercellpadding="0"cellspacing="0">
<TBODY>
<TR>
<TDclass="zi_3F6293"align="left"height="35"width="50">机 构:</TD>
<td>
<SELECTstyle="width:150px"name="groupId"id="org_id"value="${param.org_id}"onChange="change(this.options[this.options.selectedIndex].value)">
<c:forEachitems="${organizations}"var="org">
<c:iftest="${groupId==org['GroupId']}">
<OPTIONvalue="${org['GroupId']}"selected="selected">${org['GroupName']}</OPTION>
</c:if>
<c:iftest="${groupId!=org['GroupId']}">
<OPTIONvalue="${org['GroupId']}">${org['GroupName']}</OPTION>
</c:if>
</c:forEach>
</SELECT>
</TD>
</TR>
<TR>
<TDclass=zi_3F6293align="left"height="35"width="50">群 组:</TD>
<td>
<SELECTstyle="width:150Px"name="columnInfoId"id="columnInfoId">
<OPTIONvalue="ALL">---全部---</OPTION>
</SELECT>
</TD>
</TR>
<tr>
</table>
必须jar包:http://down.51cto.com/data/1009559
相关文章推荐
- Ajax 接收服务器返回的json响应方法
- jquery序列化form表单使用ajax提交后处理返回的json数据
- Spring MVC中ajax返回json数据
- ajaxFileUpload上传捕获不到服务器返回json,被加pre标签
- ajax处理php返回json数据例子
- JQuery中的ajax处理返回的Json数据
- Struts2返回JSON数据的具体应用范…
- ajaxfileupload返回json带<pre>
- AJAX示例应用-2(两级菜单的联动)-方式1(服务器以字符串形式返回子类别的id,name)
- Ajax 上传文件,返回JSON字符串在FireFox下解析带有<pre>标签的解决办法
- Struts2返回JSON数据的具体应用范例
- Struts2返回JSON数据的具体应用范例
- js获取通过ajax返回的map型的JSONArray的方法
- ajax 返回json
- spring mvc ajax 请求返回json数据
- 在ASP.NET MVC中json的应用(即JsonResult)(asp.net mvc 后台返回json数据方式 )
- Struts2+Jquery实现ajax并返回json类型数据
- Struts2 + Json _ 配置,异常解决及深入了解Struts2返回JSON数据的原理及具体应用范例
- ajax 服务端交互返回json数据处理