ajax传输json数据格式以及规范

(1)





示例:


在JsonServlet中的代码:
import java.io.IOException;
import java.io.PrintWriter;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Servlet implementation class JsonServlet
 */
@WebServlet("/JsonServlet")
public class JsonServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public JsonServlet() {
        super();
        // TODO Auto-generated constructor stub
    }


/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request, response);
}


/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("utf-8");
PrintWriter out=response.getWriter();
String str="{";
//在双引号中再次添加双引号时要用\"来表示
str+="\"stud\":[";
str+="{\"studid\":\"1001\",\"studname\":\"张三\",\"age\":\"20\"}, ";
str+="{\"studid\":\"1002\",\"studname\":\"李四\",\"age\":\"30\"}, ";
str+="{\"studid\":\"1003\",\"studname\":\"王五\",\"age\":\"10\"} ";
str+="]";
str+="}";
//上述以str开头的这几行代码在数据多时会很麻烦,所以在实际开发中会引入专门的jar包进行处理
System.out.println(str);
out.println("str");
out.flush();
out.close();


}


}


在jsp中的代码

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jQuery类库使用演示</title>
<!-- 引入jquery类库的路径pageContext.request.contextPath表示当前项目的路径 -->
   <script type="text/javascript" src="${pageContext.request.contextPath/js/jquery.js} }" ></script>


</head>
<body>
  <p id="p1">接受json数据</p>
  <input type="button"  id="btnJson" value="获取Json数据">
    <table border="1" align="center" cellpadding="3" cellspacing="1" width="500">
      <thead>
        <tr>
            <th>学号</th>
            <th>姓名</th>
            <th>年龄</th>
        </tr>
      </thead>
      <tbody id="studList"></tbody>
    
    </table>
  
       
</body>
       <script type="text/javascript">
           $(function(){
          $("#btnJson").bind("click",function(){
           var url="${pageContext.request.contextPath}/JsonServlet";
           $.post(
              url,
              {
              
              },
              function(data){
              var arr=data.stu;
              $("#p1").text(data.text.length);//这行代码可以取出
              var trs="";
              for(var i=0;i<arr.length;i++){
               trs+="<tr><td>"+arr[i].studid+"</td><td>"+arr[i].studname+"</td><td>"+arr[i].age+"</td></tr>";
              
              }
              /*
                val():对表单控件设值或者取值
                text():对非表单标签设值或者取值(字符串)
                html():会对字符串中的HTML标签解析,然后再显示
              
              */
              $("#studList").html(trs);
              
              },
              "json"
           
           );
          
          
          });
           
          
          
           });
       
       </script>
       
       
    

</html>


结果如下图:



注意:下面代码是通过引用jar进行更简便的写法,这里没用数据库,所以又建了一个StudentVo类,

































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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值