json格式实现数据传输

javaweb中Gson、fastJson、JsonLib、jackson…提供了对json传输的支持

json核心知识回顾

1.json有两种格式

(1)json对象:{key:value,key2:value2........}

 (2)json数组 :[value1,value2......]

2.json对象的解析方式 对象.key

json数组的解析方式  for循环遍历

3.java对象转换json

(1)bean或map 转换成json对象

(2) list collection集合  转换成json数组


扫描二维码关注公众号,回复: 8910377 查看本文章

一、向客户端返回json

 springMVC处理json的四个条件
* 1.导入jackson的jar包支持
* 2.在springMVC配置文件中开启mvc注解驱动  <mvc:annotation-driven>
* 3.在处理ajax请求发方法上加上注解@RequestBody
* 4.将要转换为json且相应到客户端的数据作为该方法的返回值

package com.atguigu.test;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.atguigu.dao.EmployeeDao;
import com.atguigu.domain.Employee;
/**
 * springMVC处理json的四个条件
 * 1.导入jackson的jar包支持
 * 2.在springMVC配置文件中开启mvc注解驱动  <mvc:annotation-driven>
 * 3.在处理ajax请求发方法上加上注解@RequestBody
 * 4.将要转换为json且相应到客户端的数据作为该方法的返回值
 * @ResponseBody
 * @author master
 *
 */
@Controller
public class JsonController {
    @Autowired
    private EmployeeDao employeeDao;
    
    @RequestMapping("/testJson")
    @ResponseBody
    public Collection<Employee> testJson() {
        Collection<Employee> emps = employeeDao.getAll();
        return emps;
    }

}

二、接受服务器发送过来的json数据并以表格方式展示

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>testJson here</title>
<link rel="stylesheet" href="${pageContext.servletContext.contextPath }/css/index_work.css">
<script type="text/javascript" src="${pageContext.servletContext.contextPath }/js/jquery-1.8.2.min.js"></script>
<script type="text/javascript">
    $(function(){
        $("#btn").click(function(){
            $.ajax({
                url:"testJson",
                type:"POST",
                dataType:"json",
                success:function(msg){
                    //方式一
                /*     var tb = "<table>";
                    tb +="<tr><th>id</th><th>lastName</th><th>email</th><th>gender</th><th>departmentName</th></tr>"
                        for ( var i in msg) {
                            var emp = msg[i];
                            tb +="<tr><td>"+emp.id+"</td><td>"+emp.lastName+"</td><td>"+emp.email+"</td><td>"+emp.gender+"</td><td>"+emp.department.id+"</td></tr>";
                        }
                    tb +="</table>"
                    $("body").append(tb); */
                    
                    //方式二
                    $("body").append("<table></table>");
                    $("table").append("<tr><th>id</th><th>lastName</th><th>email</th><th>gender</th><th>departmentName</th></tr>");
                    for ( var i in msg) {
                        var emp = msg[i];
                        $("table").append("<tr><td>"+emp.id+"</td><td>"+emp.lastName+"</td><td>"+emp.email+"</td><td>"+emp.gender+"</td><td>"+emp.department.id+"</td></tr>");
                    }
                    
                    
                    //alert(msg);
                    /*
                        [
                        {"id":1001,"lastName":"E-AA","email":"[email protected]","gender":1,"department":{"id":101,"departmentName":"D-AA"}},
                        {"id":1002,"lastName":"E-BB","email":"[email protected]","gender":1,"department":{"id":102,"departmentName":"D-BB"}},
                        {"id":1003,"lastName":"E-CC","email":"[email protected]","gender":0,"department":{"id":103,"departmentName":"D-CC"}},
                        {"id":1004,"lastName":"E-DD","email":"[email protected]","gender":0,"department":{"id":104,"departmentName":"D-DD"}},
                        {"id":1005,"lastName":"E-EE","email":"[email protected]","gender":1,"department":{"id":105,"departmentName":"D-EE"}}
                        ]
                    */
            /*     for ( var i in msg) {
                        var emp = msg[i];
                        alert("lastName="+emp.lastName+"email="+emp.email+"department="+emp.department+"departmentName="+emp.department.departmentName)
                    }
                     */
                    
                }
            });
        });
    });
</script>
</head>
<body>

<a href = "testJson">testJson</a>
<br/>
<br/>
<br/>
<input id="btn" type="button" value="测试Ajax">

</body>
</html>

展示结果

猜你喜欢

转载自www.cnblogs.com/lsk-130602/p/12238745.html