JSP解析JSON对象

JAVABean Emp转JSON

$(":button :first").click(function(){

url="${pageContext.request.contextPath }/emp/bean2json.do?time="+new Date().getTime();

   var sendDate =null;   $.post(url,sendDate,function(backDate,extStatus,ajax){   

   var e = ajax.responseText;

   alert(e);

   var data = eval(backDate);

   alert(data.id);

});  

});  

 

 

@RequestMapping(value="/bean2json")

public @ResponseBody Emp bean2json() throws Exception{

//创建Emp对象

Emp emp = new Emp();

emp.setId(1);

emp.setUsername("哈哈");

emp.setSalary(7000D);

emp.setHiredate(new Date());

//返回Emp对象 转成JSON文本

return emp;

}

 

 

<script type="text/javascript">

   $(":button :eq(1)").click(function(){

url="${pageContext.request.contextPath }/emp/listbean2json.do?time="+new Date().getTime();

   var sendDate =null;     $.post(url,sendDate,function(backDate){  

   var  data = eval(backDate);

   alert(data);    

   for(var p in data){

 alert(data[p].id+"-"+ data[p].username+"-"+

       data[p].salary+"-"+data[p].hiredate); }

   for(var i=0;i

   for(var key in data[i]){

   alert(key+':'+data[i][key]);

   }

   }  

   });  

   });

  script>

 

 

@RequestMapping(value="/listbean2json")

public @ResponseBody List listbean2json() throws Exception{

//创建List对象

List empList = new ArrayList();

//向List对象中添加三个Emp对象

empList.add(new Emp(1,"哈哈",7000D,new Date()));

empList.add(new Emp(2,"呵呵",8000D,new Date()));

empList.add(new Emp(3,"嘻嘻",9000D,new Date()));

//返回需要转JSON文本的对象

return empList;

}

  <script type="text/javascript">

   $(":button :last").click(function(){

url="${pageContext.request.contextPath}/emp/map2json.do?time="+new Date().getTime();

   var sendDate =null;  

   $.post(url,sendDate,function(backDate,extStatus,ajax){      

  //var  data = eval(backDate);

  //var row = data.rows;

       //alert(row);

         var row = backDate.rows;

   alert(backDate.total);  

   for(var p in row){

   alert(row[p].id+"-"+ row[p].username+"-"+row[p].salary+"-"+row[p].hiredate);

   }  

   });   

   });  

script>

 

 

@RequestMapping(value="/map2json")

public @ResponseBody Map map2json() throws Exception{

//创建List对象

List empList = new ArrayList();

//向List对象中添加三个Emp对象

empList.add(new Emp(1,"哈哈",7000D,new Date()));

empList.add(new Emp(2,"呵呵",8000D,new Date()));

empList.add(new Emp(3,"嘻嘻",9000D,new Date()));

//创建Map对象

Map map = new LinkedHashMap();

//向Map对象中绑定二个变量

map.put("total",empList.size());

map.put("rows",empList);

//返回需要转JSON文本的对象

return map;

}

你可能感兴趣的:(JSP解析JSON对象)